6

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

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

Here's how I read:

template of static function f called with (ChT<int Fallback::*, &C::x>*), but then I can't make sense why is there an address-of operator and why is there an array?

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

Community
  • 1
  • 1
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194

2 Answers2

7

Using some typedefs:

typedef char (&arrayref_t)[1];

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

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

This is a template class, instantiated with the type "pointer to an int member of the Fallback class", and a member pointer to x in class C.

static arrayref_t f(tmpl_t*);

The function now takes a pointer to a tmpl_t and returns an arrayref_t.

sth
  • 222,467
  • 53
  • 283
  • 367
  • I've just observed VERY interesting thing (yes, I'm still munching it). Where you're saying "This is ..., and a member pointer to x in class C", it should be mentioned that this member pointer in class C is actually member of class Fallback due to declaration of class ChT. So in a fact they are connected with each other. – There is nothing we can do Mar 17 '10 at 17:05
3

It's important to see return type. So, return type of this function is reference to char[1]; Imagine that f returns something like reference 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;
}
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88