9

One of my personal experiments to understand some of the C++0x features: I'm trying to pass a function pointer to a template function to execute. Eventually the execution is supposed to happen in a different thread. But with all the different types of functions, I can't get the templates to work.

#include <functional>

int foo(void) {return 2;}

class bar {
public:
    int operator() (void) {return 4;};
    int something(int a) {return a;};
};

template <class C>
int func(C&& c)
{
    //typedef typename std::result_of< C() >::type result_type;
    typedef typename std::conditional< 
        std::is_pointer< C >::value,
        std::result_of< C() >::type,
        std::conditional<
            std::is_object< C >::value,
            std::result_of< typename C::operator() >::type,
            void>
        >::type result_type;
    result_type result = c();
    return result;
}

int main(int argc, char* argv[])
{
    // call with a function pointer
    func(foo);

    // call with a member function
    bar b;
    func(b);

    // call with a bind expression
    func(std::bind(&bar::something, b, 42));

    // call with a lambda expression
    func( [](void)->int {return 12;} );

    return 0;
}

The result_of template alone doesn't seem to be able to find the operator() in class bar and the clunky conditional I created doesn't compile. Any ideas? Will I have additional problems with const functions?

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
user328543
  • 577
  • 3
  • 9
  • Perhaps the reason it's not compiling is because `func` returns `int` instead of `result_type` (which in this example is `int` in all cases but may still cause a problem)? – Chris Lutz Apr 29 '10 at 07:06
  • @Chris: No that's not the reason. – kennytm Apr 29 '10 at 07:08
  • @KennyTM - I thought not, but still think it's a problem. – Chris Lutz Apr 29 '10 at 07:09
  • The reason it's not compiling is a typo. You typed capital `C` (which is the class) when you meant a lowercase `c` (which is the parameter to the function). You still got some answers that will probably make your code cleaner, so I'd feel bad posting this as an answer. – Chris Lutz Apr 29 '10 at 07:14
  • @Chris, i don't see where that would be a problem? – Georg Fritzsche Apr 29 '10 at 07:25
  • @gf - `class()` is different from `class x; x();`. OP wants the second, but has written the first. – Chris Lutz Apr 29 '10 at 07:30
  • @Chris: But the call `c()` uses lower-case `c`? – Georg Fritzsche Apr 29 '10 at 07:35
  • @gf - The actual call in the code does, but in both your answer and the OP's code, `std::result_of` uses capital `C()`. – Chris Lutz Apr 29 '10 at 08:57
  • @Chris: Thats how it works, its implemented as a plain class template and you have to pass types to it - there is nothing getting instantiated. – Georg Fritzsche Apr 29 '10 at 10:34
  • I guess the std::result_of your C++ implementation is not up to date (only partial C++0x support). The "real" std::result_of should be able to deal with that. – sellibitze Apr 30 '10 at 19:54

3 Answers3

6

How about using decltype?

template <class C>
auto func(C&& c) -> decltype(c()) {
    auto result = c();
    return result;
}
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
4

If i understand the C++0x draft right the following should actually be sufficient:

typedef typename std::result_of<C()>::type result_type;

Using that instead of your conditional expression, it compiles fine on gcc4.5 - maybe you have found a bug in whatever compiler you are using?

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • thats what I thought too, but it didn't compile. The entire complex conditional was created to work around that. – user328543 Apr 29 '10 at 19:07
  • 1
    @user328543: If i comment out your conditional and use the line previously commented out it compiles absolutely fine on gcc4.5. – Georg Fritzsche Apr 29 '10 at 20:09
2

I got your template to instantiate, but GCC complains about possibly every use of result_of.

template <class C>
int func(C&& c)
{
    //typedef typename std::result_of< C() >::type result_type;
    typedef typename std::conditional<
        std::is_pointer< C >::value,
         // C++0x still requires "typename" sprinkles:
        typename std::result_of< C() >::type,
        typename std::conditional<
            std::is_object< C >::value,
             // result_of takes a *type* as an argument, not an object:
            //typename std::result_of< decltype( &C::operator() ) >::type,
             // Or better:
            typename std::result_of< C >::type,
            void>
        >::type result_type;
    result_type result = c();
    return result;
}

int main(int argc, char* argv[])
{
    // according to GCC, func(foo) passes a function reference.
    func(foo);

First error message:

rof.cpp:23:17: error: invalid use of incomplete type 'struct std::result_of<int (&)()>'

result_of is implemented as specified in the standard, so it appears that GCC cannot match the pseudo-prototype syntax in the partial specialization declaration.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • References to functions should work: *"Fn shall be a function object type (20.8), reference to function, or reference to function object type."* – Georg Fritzsche Apr 29 '10 at 07:33
  • 1
    @gf: cool, well I'll keep the comment as usefully pedantic. Either way, GCC's error message clearly shows it's rejecting good code. – Potatoswatter Apr 29 '10 at 07:35