c++ - 模板函数参数和模板成员函数参数

标签 c++ templates

我有这个问题:

template <void (*F)(int)> struct FunctionWrapper // This compiles and works
{
    static void call_it() 
    { 
        F(0); 
    }
};

class MyClass
{
public:
    static void callMe(int k)
    {
    }
};

template <void (MyClass::*F)(int)> struct FunctionWrapper // Error - F incompatible with declaration
{
    static void call_it() 
    { 
        MyClass::F(0); 
    }
};

为什么我可以使用函数指针(编译时常量),但不能使用类成员(甚至静态)指针?

最佳答案

静态函数与普通函数具有相同的类型签名;它并不是真正的成员函数。

关于c++ - 模板函数参数和模板成员函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176060/

相关文章:

c++ - 简单的更改导致 make : Error 1

c++ - 如果模板化的 "ResourceCache"需要不同的创建参数/方法,如何加载/创建 T 类型的资源?

asp.net-mvc - ASP.NET MVC 3 中的条件布局

c++ - AcceptEx 返回 1022 (WSAEINVAL)...我做错了什么?

c++ - 这个后增量语句会导致未定义的行为吗?

c++ - Const 正确性、std 移动和智能指针

templates - Golang setCookie() 在模板 Execute() 之后失败

c++ - 如何减少模板参数?

c++ - VS 2013 模板不编译

c++ - std::map 中的最后一个键