Strange declaration (templates). C ++

How can I understand what is posted here: (this is taken from another post on this forum )

template<typename C> static char (&f(ChT<int Fallback::*, &C::x>*))[1]; 

Here is how I read:

the template of the static function f called with (ChT<int Fallback::*, &C::x>*) , but then I can’t understand why the operator address exists and why the array exists?

I'm still learning how to understand C ++ declarations, so please explain it slowly and carefully.

+6
c ++ templates
source share
2 answers

It is important to see the type of return. Thus, the return type of this function refers to char[1] ; Imagine f returning something like a link to the following:

 char ret[1]; 

for example

 template<typename C> static char (&f(ChT<int Fallback::*, &C::x>*))[1] { static char xx[1] = {'F'}; return xx; } 
+3
source share

Using some typedefs:

 typedef char (&arrayref_t)[1]; 

This is a reference to an array of characters. An array has one element.

 typedef ChT<int Fallback::*, &C::x> tmpl_t; 

This is a template class created with the type "pointer to a member of int Fallback class" and a pointer to x in class C

 static arrayref_t f(tmpl_t*); 

Now the function takes a pointer to tmpl_t and returns arrayref_t .

+7
source share

All Articles