C++ 作为对象成员或模板参数的不同行为

C++ 作为对象成员或模板参数的不同行为,c++,templates,C++,Templates,我曾尝试为成员函数指针创建模板帮助器,其主要思想是能够执行以下操作: template <typename T, typename R> struct TStruct { typedef T value_type; typedef R return_type; R Function(T &); }; struct Struct { int Function(const int &); }; typedef TStruct<c

我曾尝试为成员函数指针创建模板帮助器,其主要思想是能够执行以下操作:

template <typename T, typename R> struct TStruct
{
    typedef T value_type;
    typedef R return_type;

    R Function(T &);
};

struct Struct
{
    int Function(const int &);
};

typedef TStruct<const int &, int> A;

FunctionPointer<A, A::return_type, A::value_type>::pointer p = &A::Function;
typedef FunctionPointer<Struct, int, const int &>::pointer FPointer;
但是如果使用了
Bad
结构,则会出现编译错误:

int main(int argc, char **argv)
{
    // invalid initialization of reference of type ‘const int&’
    // from expression of type ‘int (TStruct<const int&, int>::*)(const int&)’
    Bad<A, A::return_type, A::value_type, &A::Function>::pointer BadTStructPtr = &A::Function;

    // invalid initialization of reference of type ‘const int&’
    // from expression of type ‘int (Struct::*)(const int&)’
    Bad<Struct, int, const int &, &Struct::Function>::pointer BadStructPtr = &Struct::Function;

    return 0;
}
int main(int argc,char**argv)
{
//“const int&”类型引用的初始化无效
//来自“int(tsstruct::*)(const int&)”类型的表达式
Bad::pointer BadTStructPtr=&A::函数;
//“const int&”类型引用的初始化无效
//来自“int(Struct::*)(const int&)”类型的表达式
错误::指针BadStructPtr=&Struct::函数;
返回0;
}
好的,
Good::pointer
Bad::pointer
都是
Return(type:*)(Parameter)
类型,所以它们必须能够指向
&A::Function
Struct::Function
,但我显然错了,但我不知道为什么。在将类型声明为模板化对象体和将其声明为模板参数时,似乎有不同的行为

所以问题是:为什么不将
坏的::指针
作为指向
&A::function
&Struct::function
的函数指针使用?

你的意思是什么

template <typename Type, 
          typename Return, 
          typename Parameter, 
          Return (Type::*Pointer)(Parameter)> struct Bad
{
    typedef Pointer pointer;
    //      ^^^^^^^ (not Parameter)
};
模板结构错误
{
typedef指针;
//^^^^^^^^(非参数)
};

erm。。。也许我看错了,但“Bad”的结构体是否应该不是typedef指针
typedef参数指针
看起来非常错误。天哪!这只是一个打字错误!是的,这是一个打字错误。@PaperBirdMaster:错误消息通常包含足够的信息来理解这一点。一旦你知道问题是什么,再看一遍错误信息,试着理解编译器说的话。你完全正确,但是经过一天的努力,我很傻,顺便说一句,debo mejorar mi Compressionón del inglés;)
int main(int argc, char **argv)
{
    // invalid initialization of reference of type ‘const int&’
    // from expression of type ‘int (TStruct<const int&, int>::*)(const int&)’
    Bad<A, A::return_type, A::value_type, &A::Function>::pointer BadTStructPtr = &A::Function;

    // invalid initialization of reference of type ‘const int&’
    // from expression of type ‘int (Struct::*)(const int&)’
    Bad<Struct, int, const int &, &Struct::Function>::pointer BadStructPtr = &Struct::Function;

    return 0;
}
template <typename Type, 
          typename Return, 
          typename Parameter, 
          Return (Type::*Pointer)(Parameter)> struct Bad
{
    typedef Pointer pointer;
    //      ^^^^^^^ (not Parameter)
};