我想在模板函数中添加调试,但不考虑重新编辑整个代码。
可以吗
#define theFunction<T>(size) _theFunction<T>(size, __FILE__, __LINE__)
template<class T> T* _theFunction(int size, string file, int line)
{
if (fails) {
printf("theFunction failed called at line %i on %s ", line, file);
}
}
当然会在宏声明中返回意外的“<”。有一些技巧可以实现这一点吗?
最佳答案
也许是这样的(未测试):
struct TheFunctionHelper {
std::string file;
int line;
template<typename T>
T* invoke(int size) {
return _theFunction<T>(size, file, line);
}
};
#define theFunction TheFunctionHelper{__FILE__, __LINE__}.invoke
关于c++ - #定义一个模板方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65291188/