Getting the Return Type

I know most of the C++ Templates Functionalities, but I can't figure out at all how to get the return type of a function.
I already know about specializations, but I don't think I want to specialize a return value for each type.

Is there any way?
it depends: if you need to get the type of a function call expression, then use std::result_of
http://www.cplusplus.com/reference/type_traits/result_of/
http://en.cppreference.com/w/cpp/types/result_of

If you need to get the return type of a function type, then it's a simple specializatoin:

1
2
3
4
5
template <typename R, typename... Args>
struct return_type<R(Args...)>
{
    typedef R type;
};
Last edited on
I see, not only from this post, you updated to C++0x.
Variadic template arguments aren't supported.
Sadly I cannot atm, Isn't there any way in C++ '03?
I guess I should specialize for a multiple amount of parameters?

Thanks for std::result_of, I didn't know about that feature of C++0x, it will be useful once i'll get to it.
I guess I should specialize for a multiple amount of parameters?

Yes, that's what boost does on old compilers.

You can also use boost.result_of (that's where C++ got it from)

http://www.boost.org/doc/libs/release/libs/utility/utility.htm#result_of
Last edited on
Perfect, That's enough, Question answered, in the hope of it being compatible with the 2003 revision.
Topic archived. No new replies allowed.