4
#include <type_traits>
using namespace std;

struct asd{
    void f();
};

int f();

typedef typename result_of<decltype(f)>::type result_free;
typedef typename result_of<decltype(&asd::f)>::type result_mem;

both the typedefs give an error

In file included from ../main.cpp:1:0:
/usr/include/c++/4.6/type_traits: In instantiation of ‘std::_Result_of_impl<false, false, int>’:
/usr/include/c++/4.6/type_traits:1215:12:   instantiated from ‘std::result_of<int()>’
../main.cpp:10:41:   instantiated from here
/usr/include/c++/4.6/type_traits:1192:9: error: ‘std::declval [with _Tp = int, typename std::add_rvalue_reference<_Tp>::type = int&&]()’ cannot be used as a function
../main.cpp:10:43: error: invalid combination of multiple type-specifiers
../main.cpp:10:59: error: invalid type in declaration before ‘;’ token
../main.cpp:11:49: error: ‘type’ in ‘struct std::result_of<void (asd::*)()>’ does not name a type
../main.cpp:11:64: error: invalid type in declaration before ‘;’ token
ildjarn
  • 62,044
  • 9
  • 127
  • 211
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65

1 Answers1

6

result_of is result_of<Fn(ArgTypes...)>, not just result_of<Fn>;

Try

typedef typename result_of<decltype(&f)()>::type result_free;
typedef typename result_of<decltype(&asd::f)(asd)>::type result_mem;

(works with gcc 4.6.2)

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • why doesn't this code compile even if I append a `()` after the `T` in the line that causes the error? http://ideone.com/pVbBz I am lost, I thought that the purpose of a `decltype(&f)` was to get the type of the "variable" f, and if I have a template argument instead of `decltype` it should be ok... – Lorenzo Pistone Feb 03 '12 at 17:31
  • @LorenzoPistone The example has two obvious errors: trying to use `result_of`, which does not exist, and taking the address of `main`. As for the failure to use "function returning a function", which you'd get if you `result_of`, where T is a function type, that's a good question. – Cubbi Feb 03 '12 at 18:52
  • @LorenzoPistone nevermind, it's just that result_of does not take function types, which `T` is in your case. To quote the standard, `Fn shall be a callable type, reference to function, or reference to callable type.`. If you `std::add_lvalue_reference` and don't forget the parentheses, it will work. – Cubbi Feb 03 '12 at 18:59