std::result_of

1
2
3
4
5
6
7
8
#include <iostream>
#include <type_traits>

int f(int) {return 2;} 

int main() {
  typedef std::result_of<decltype(f)&(int)>::type A;
}


Can someone explain why
 
typedef std::result_of<f(int)>::type A;

does not compile? Why is decltype and the reference & needed?
> why typedef std::result_of<f(int)>::type A; does not compile?

f is not a type (it is a function).


> Why is decltype and the reference & needed?

decltype is needed because decltype(f) is a type - int(int)

reference - either std::result_of< decltype( (f) )(int) >::type
or std::result_of< decltype(f)&(int) >::type - is needed because we can't return a function by value.
Functions are not copyable or moveable types.

We can return a reference (or a pointer) to a function.

decltype((f))(int) - type is function returning reference to function; same as decltype(f)&(int).
decltype(&f)(int) - type is function returning pointer to function; same as decltype(f)*(int).
Topic archived. No new replies allowed.