简体   繁体   中英

How to create a macro for a templated function in c++

I want to create a macro for a template function
something on the lines of

#define CHECK_MIN(T)(value, target) checkmin(T value, T target) \
checkmin<T>(T val, T target, __FUNCTION__, __LINE__)

template<typename T> checkmin(T val, T target, const char* functionName, long lineNumber)
{
    // Check if the val is less than target
    // Construct a std::string using the function name and line name
    // throw std:: exception passing the string constructed above.
}

I am not able to get the syntax to achieve this. Any ideas?

You can do it by following way:

#include <iostream>
using namespace std;

template<typename T>
 void  checkmin(T val, T target, const char* functionName, long lineNumber)
 {
   // Check if the val is less than target
   // Construct a std::string using the function name and line name
   // throw std:: exception passing the string constructed above.
 }

#define CHECK_MIN(value, target)  \
 checkmin(value,target, __FUNCTION__, __LINE__);

int main() {
    /*Not using new in object definitions so I don't have to delete them afterwards since pointers don't stay in memory*/
    CHECK_MIN(5,6);

     return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM