class IsClassT { private...">

Where can I find a description about using "int C :: *"?

In a piece of code, I find something like:

template<typename T> class IsClassT { private: typedef char One; template<typename C> static One test(int C::*); ... 

The question is, where can I find a description of why using the "int C :: *" function is really in the definition of the test () function?

+8
c ++ stl
source share
3 answers

I will not describe what int C::* means, since @Charles Bailey has already done this very well. However, I will answer your question:

(...) why is the use of the function "int C :: *" permissible in the test () function definition?

The key point is that using int C::* (a pointer to a member of type int ) is valid if and only if C is a class type. Otherwise, the int C::* poorly formed.

That's why you have

 template<typename C> static One test(int C::*); 

and most likely somewhere below

 template <typename> static Two test(...); static const bool value = sizeof(test<T>(0)) == 1; 

When test<T>(0) considered by the compiler, it checks candidates for test . He finds two:

 template<typename C> static One test(int C::*); template <typename> static Two test(...); 

The first takes precedence over the second, because 1) they are both functions of the template, and 2) the ellipsis is viewed last. If the first one is poorly formed (i.e., if and only if C not a type of class), then it is simply discarded , and the second overload is taken. This behavior is called SFINAE (for failure of substitution is not an error).

Testing the size of the return type (remember that sizeof(char) always 1), you can evaluate at compile time that test is taken, i.e. whether type T a class type or not.

+6
source share

int C::* is a pointer to a C element of type int . Find the "member pointer". The section of the standard (ISO / IEC 14882: 2003) that deals with this declaration syntax is 8.3.3 Member Pointers [dcl.mptr].

Usage example.

 struct Example { int a; int b; }; int test( Example& ex, int Example::* p ) { return ex.*p; } int main() { Example x = { 3, 5 }; // Convoluted way of extracting xa and xb int a = test( x, &Example::a ); int b = test( x, &Example::b ); } 
+10
source share

This is a pointer to a member. A simple example for understanding A pointer to a member .

 class A { int a; int b; void DoSomething(); }; int main() { A *ObjPtr; //pointer to member a int A::*ptr = &A::a; //Usage objPtr->*ptr = NULL; //pointer to member function void (A::*FuncPtr)(void) = &A::DoSomething; //Usage (objPtr->*FuncPtr)(void); return 0; } 
+2
source share

All Articles