在Visual Studio 2008中使用指向模板函数的指针编译错误

Jer*_*rry 11 c++ visual-studio-2008

我正在尝试创建一个模板类,它存储一个模板函数的函数指针,但在Visual Studio 2008中遇到了编译错误.我为它创建了一个简化的测试用例(见下文),它仍然无法在VS2008中编译,但似乎在我尝试的在线Comeau和在线GCC编译器上成功编译.

我看到的错误是:

error C2436: 'func' : member function or nested class in constructor initializer list
temp.cpp(21) : while compiling class template member function 'test_class<T>::test_class(T (__cdecl &))'
1>        with
1>        [
1>            T=int (const int &)
1>        ]
Run Code Online (Sandbox Code Playgroud)

使用非模板功能的相同测试有效.除此之外,是否有人知道该问题的解决方法,或者VS2008是否期望某种不同的语法?

谢谢,

杰瑞

template<class T>
T template_function(const T& arg)
{
    return arg;
}

int non_template_function(const int& arg)
{
    return arg;
}

template<class T>
class test_class
{
public:
    test_class(const T& arg) : func(arg) {}
private:
    T func;
};

template<class T>
void create_class(const T& arg)
{
new test_class<T>(arg);
}

int main()
{
    create_class(&template_function<int>); //compile fails unless this is commented out
    create_class(&non_template_function);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*Jon 0

在我看来, test_class 和 create_class 中的“const T& arg”是你的问题。将它们更改为简单的“T arg”似乎可以使事情变得顺利。