8

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, and where conversion-type-id denotes the type “pointer to function of (P1,...,Pn) returning R”, or the type “reference to pointer to function of (P1,...,Pn) returning R”, or the type “reference to function of (P1,...,Pn) returning R”, a surrogate call function with the unique name call-function and having the form

R call-function ( conversion-type-id F, P1 a1, ... ,Pn an) { return F (a1,... ,an); }

is also considered as a candidate function. Similarly, surrogate call functions are added to the set of candidate functions for each non-explicit conversion function declared in a base class of T provided the function is not hidden within T by another intervening declaration.


Question #2: In C++11, can such a conversion be specified using a default template argument? This is useful for SFINAE. The only difference here from the above example is that the conversion-type-id only represents a function reference after instantiation, because it's a dependent type (despite invariance). This trips up GCC and it skips the member template.

enum { call_alternate = true; }

struct call_switch {
    template< bool en = call_alternate >
    operator typename std::enable_if< en, decltype( fn_1 ) & >::type ()
        { return fn_1; }

    template< bool en = ! call_alternate >
    operator typename std::enable_if< en, decltype( fn_2 ) & >::type ()
        { return fn_2; }
};

We also have alias templates. It seems that alias substitution occurs before instantiation, given the example in §14.5.7/2, where the declarations of process conflict. In GCC 4.7, this code at least instantiates the declaration, but then it produces a bizarre "candidate expects 2 arguments, 2 provided" error.

template< typename t >
using fn_t = void (&)( t );

struct talk {
    template< typename t >
    operator fn_t< t >() { return fn; }
};

int main() {
    talk()( 3 );
}
4
  • What are you trying to do? Where can it be useful?
    – Nawaz
    Jun 13, 2012 at 16:33
  • @Nawaz when you want a conversion operator to function pointer types? Jun 13, 2012 at 16:35
  • @Nawaz The first example under question 2 is how I got into this. The conversion function just seems nicer than perfect forwarding. When I checked the GCC source to see why it didn't work, I found a function add_template_conv_candidate which appears to be unreachable due to the syntactic quirks. So I want to understand this dark corner of the language. Jun 13, 2012 at 17:37
  • Filed GCC bug: gcc.gnu.org/bugzilla/show_bug.cgi?id=53660 Jun 13, 2012 at 19:31

1 Answer 1

3

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.

Yes, that is correct.

Question #2: In C++11, can such a conversion be specified using a default template argument?

It can, and you can also use alias templates, but you cannot use such a conversion function template for creating surrogate call functions. You can use it for converting your class object to function pointers in implicit conversions otherwise.

We also have alias templates. It seems that alias substitution occurs before instantiation, given the example in §14.5.7/2, where the declarations of process conflict. In GCC 4.7, this code at least instantiates the declaration, but then it produces a bizarre "candidate expects 2 arguments, 2 provided" error.

Yes, this is https://groups.google.com/forum/?fromgroups#!topic/comp.std.c++/lXLFBcF_m3c (and caused closure of DR395), but even though such a conversion function template can work in cases like void(&p)() = yourClassObject, it won't work for surrogate call functions, because there the conversion function needs to provide a fixed non-dependent type that the class object is converted to when the surrogate function is called, but a conversion function template does not provide such a type normally (weird things like template<typename = int> operator Identity<void(*)()>(); aside...).

I think that GCC may incorrectly generates the candidate call-function(void (&)( t ), t) with the dependent types still there and try to call that candidate, thereby violating some invariant of it (which could explain the weird error message - possibly hitting an } else { ... } unexpectedly somewhere).

5
  • Great answer, except "should be clear" why? The wording "conversion function declared in T of the form operator conversion-type-id ()" doesn't say anything about not being a template. Jun 13, 2012 at 21:00
  • @Potatoswatter i think that is an omission in the spec. But it just makes no sense to me to consider conversion function templates here. What should be the semantics? Jun 13, 2012 at 21:03
  • As long as the function type can be deduced from the argument list using overload resolution, then no special semantics are needed. That's just what GCC is doing (halfway). Essentially the surrogate call function becomes a surrogate call function template. Jun 13, 2012 at 21:08
  • @Potatoswatter there will always be a deduction failure because the first parameter will always mismatch. In your case, void(&)(t) mismatches type talk (implied object argument). The argument from the committee will probably be "It says conversion function. A conversion function template is not a conversion function.". Although a more clear text from the spec would probably be better. Jun 13, 2012 at 21:15
  • Perhaps. Of course, I mean that the same transformation is applied to the template as would be to the function. There's no need for an actual surrogate, this is all just as-if. Jun 13, 2012 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.