Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member to function pointer inside template class gives error: must be a class or namespace when followed by '::'

I'm trying to declare a template function pointer in C++.

template <class T>
class MyClass
{
public:
    typedef const unsigned char* (T::*MyTemplatedEvent)(unsigned long &myParameter);
};

but for some reason I keep getting this error:

'T': must be a class or namespace when followed by '::'

Can somebody tell what I'm doing wrong?
the compiler should know that T is a class. It says so above the MyClass declaration...

like image 367
Idov Avatar asked Jun 02 '12 14:06

Idov


2 Answers

With T::*MyTemplatedEvent, you are expecting T to be a class type, since only class types can have member pointers. This means that if you pass a non-class type, say int or char*, you get the indicated error, as there are no members and conversely no member pointers for those types.

the compiler should know that T is a class. It says so above the MyClass declaration...

Wrong. class T is the same as typename T in a template parameter, and only tells the compiler that T is a placeholder for any type that is passed as a template argument later on. It does not restrict the type to be of class type.

like image 65
Xeo Avatar answered Sep 27 '22 17:09

Xeo


I'm guessing it's because I try to create "MyClass" of template which is a pointer or primitive which the compiler know can't have and function pointer associated with them...

like image 43
Idov Avatar answered Sep 27 '22 18:09

Idov