C++ is an object oriented language and is strongly typed. This means that every value (rvalue, not just lvalue) has a type. That's how the language knows what you mean to do on the value.

Classes are not objects in C++, but that's OK, because they're never values either. Still, some things in C++ just don't have a type! The example below comes from Andrei Alexandrescu's book Modern C++ Design, and involves pointers to member functions.

OK, so lets say I have this code (no joking, this really is C++!):

Object o,*oo;     // Some objects
int (Object::*ptr_to_o_func)(double);
              // ptr_to_obj_func is a pointer to a
              // member function of Object

oo = getObjectPtr();

ptr_to_o_func = (some boolean condition) ?
  &Object::foo : &Object::bar;

std::cout << "Value is " <<
  (o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
  << endl;
Apart from the atrocious syntax (especially the weird constructs ::*, .* and ->*), this is fundamentally weird.

Note that o.*ptr_to_o_func and oo->*ptr_to_o_func are functors: they're objects for which operator()(double) is defined. So what's their type? Well, they don't have one! They're strange closures which bind together an object and a pointer to one of its member functions. If they had a type, you could store this "frozen function" in a variable of that type. But that would be too weird. So these two values have no type! Which doesn't prevent the compiler from type-checking their use, of course (you cannot pass oo->*ptr_to_o_func anything except a double, and you'll receive an int in return).

So much for strong typing...

Log in or registerto write something here or to contact authors.