获取在 C++11 中具有可变参数模板的函数的地址
Get address of a function which has variadic templates in C++11
使用可变参数模板保存函数地址时,g++ 编译器(版本 4.8.2)输出此错误:
address of overloaded function with no contextual type information
有问题的代码:
template<typename... Args>
void redirect_function(const char *format, Args... args)
{
pLog->Write(format, args...); // or: printf(format, args...);
}
void *fnPtr = (void *)&redirect_function; // The error occurs here.
这是我在其他地方用这个做的:
typedef void (*log_bridge)(const char*, ...);
log_bridge LogWrite;
LogWrite = (log_bridge)fnPtr;
我没有其他可能性,所以请不要提出完全不同的解决方法。
嗯。为什么这是不可能的很简单。你有明显的歧义。 redirect_function
不是函数;作为所有模板函数,它更像是一组从模板为不同类型的参数生成的重载。
该函数需要先实例化才能获取其地址,而您没有为此提供必要的信息。
换句话说,问题是您不可能知道应该在有问题的行上使用 redirect_function
的哪个具体重载。
您唯一能做的就是明确提供模板参数。
使用可变参数模板保存函数地址时,g++ 编译器(版本 4.8.2)输出此错误:
address of overloaded function with no contextual type information
有问题的代码:
template<typename... Args>
void redirect_function(const char *format, Args... args)
{
pLog->Write(format, args...); // or: printf(format, args...);
}
void *fnPtr = (void *)&redirect_function; // The error occurs here.
这是我在其他地方用这个做的:
typedef void (*log_bridge)(const char*, ...);
log_bridge LogWrite;
LogWrite = (log_bridge)fnPtr;
我没有其他可能性,所以请不要提出完全不同的解决方法。
嗯。为什么这是不可能的很简单。你有明显的歧义。 redirect_function
不是函数;作为所有模板函数,它更像是一组从模板为不同类型的参数生成的重载。
该函数需要先实例化才能获取其地址,而您没有为此提供必要的信息。
换句话说,问题是您不可能知道应该在有问题的行上使用 redirect_function
的哪个具体重载。
您唯一能做的就是明确提供模板参数。