Difference between std::result_of and decltype
Posted
by Luc Touraille
on Stack Overflow
See other posts from Stack Overflow
or by Luc Touraille
Published on 2010-04-22T09:45:52Z
Indexed on
2010/04/22
9:53 UTC
Read the original article
Hit count: 158
I have some trouble understanding the need for std::result_of
in C++0x. If I understood correctly, result_of
is used to obtain the resulting type of invoking a function object with certain types of parameters. For example:
template <typename F, typename Arg>
typename std::result_of<F(Arg)>
invoke(F f, Arg a)
{
return f(a);
}
I don't really see the difference with the following code:
template <typename F, typename Arg>
auto invoke(F f, Arg a) -> decltype(f(a)) //uses the f parameter
{
return f(a);
}
or
template <typename F, typename Arg>
auto invoke(F f, Arg a) -> decltype(F()(a)); //"constructs" an F
{
return f(a);
}
The only problem I can see with these two solutions is that we need to either:
- have an instance of the functor to use it in the expression passed to decltype.
- know a defined constructor for the functor.
Am I right in thinking that the only difference between decltype
and result_of
is that the first one needs an expression whereas the second does not?
© Stack Overflow or respective owner