Extract the return type of a function without calling it (using templates?)

后端 未结 7 1697
温柔的废话
温柔的废话 2020-12-16 21:49

I\'m looking for a way in C++ to extract the return type of a function (without calling it). I presume this will require some template magic.

float Foo();
i         


        
相关标签:
7条回答
  • 2020-12-16 22:54

    As suggested by dribeas, here is the solution I eventually came to:

    float Foo();
    int Bar();
    
    template<typename T> Monkey(T) {
        boost::function_traits< boost::remove_pointer<T>::type >::result_type var1 = ...;
        // Now do something
    }
    
    Monkey(Foo);
    Monkey(Bar);
    

    This isn't exactly the form I aimed for with my original question, but it's close enough for me.

    0 讨论(0)
提交回复
热议问题