Yay, another question title composed of a random sequence of C++ terms!

Usually we make a class Callable by implementing operator(). But you can also do so by implementing a user-defined conversion to function pointer or reference type. Instead of using perfect forwarding, a conversion function can return a pointer to a function which is then called with the original argument list.

struct call_printf {
    typedef int printf_t( char const *, ... );
    operator printf_t & () { return std::printf; }
};

http://ideone.com/kqrJz

As far as I can tell, the typedef above is a syntactic necessity. The name of a conversion function is formed from a type-specifier-seq, which does not allow a construct like int (*)(). That would require an abstract-declarator. Presumably the reason is that such type names get complicated, and complex constructs used as object names are tough to parse.

Conversion functions are also allowed to be templated, but the template arguments must be deduced, because there is nowhere to explicitly specify them. (That would defeat the whole point of implicit conversion.)


Question #1: In C++03, is there was no way to specify a function conversion operator template? It appears there was no way to resolve the template arguments (i.e., name them in a deduced context) in an acceptable function pointer type.

Here is the equivalent reference from C++11, ยง13.3.1.1.2/2 [over.call.object]. It's substantially the same from C++03:

In addition, for each non-explicit conversion function declared in T of the form

operator conversion-type-id () cv-qualifier attribute-specifier-seqopt;

where cv-qualifier is the same cv-qualification as, or a greater cv-qualification than, cv,

Edit
Report