Questions tagged [result-of]

std::result_of is a C++11 metafunction that provides the result of calling a function type with a given set of argument types.

The class template std::result_of is defined in the C++11 standard library and is a type transformation trait i.e. a metafunction that takes one type and produces another type.

The nested type std::result_of<F(A, B, C)>::type is a typedef for the type that would be returned by the expression f(a, b, c) for a callable object f of type F and arguments of types A, B and C.

50 questions
4
votes
1 answer

Getting the Return Type of a Templatized Object's Method

Say that I have: template struct Foo { T& func(); }; And I implement a Foo: Foo bar Now I want to get the return type of bar.func(). I've been trying to force result_of to work with me but to no avail. What I'd really like is…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
1 answer

Get return value for template lambda parameter, how to simplify code?

This is my trick: template auto get_return_value(F * f = NULL, TArg * arg = NULL) -> decltype((*f)(*arg)); Example of using: template decltype(get_return_value()) applyFtoT(F f, T t) { …
k06a
  • 17,755
  • 10
  • 70
  • 110
3
votes
0 answers

Variadic template for decuction for member parameter

I have class that wraps a std::future. He being the result of an async call on a generic function specified by template and the args by a variadic template. template class AsyncTask { public: AsyncTask(const Fn&…
3
votes
3 answers

error: `type` in `class std::result_of` does not name a type

Example below fails with all compilers I've tried: gcc-8.2, clang-8.0 (both options --std=c++17 and std=c++2a were tried) and zapcc-2017.08. From my point of view the code sample is valid and should be compiled. Or, at least, there should be a more…
hutorny
  • 863
  • 11
  • 17
3
votes
2 answers

gcc 6.1 std::result_of compilation error

Consider a small standalone use case wherein I want to determine if a type is a complete or incomplete using #include namespace { struct foo { template std::false_type operator()(T&); …
Recker
  • 1,915
  • 25
  • 55
2
votes
3 answers

Avoiding Repetition For SFINAE Differentiating Between void and Non-void Return Types

Some generic code manipulating functions, need to operate differently depending on whether a function has a return value or not. E.g., borrowing a problem from this question, say we need to write a time_it function takes a function and some…
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
2
votes
1 answer

How Can I Use result_of Instead of decltype?

In this answer I create a type trait: template using to_string_t = decltype(to_string(declval())); This works just fine but I originally set out to use result_of and now it's irking me that I can't figure out how to do it. I'm trying…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
1 answer

Unexpected SFINAE failure using std::result_of

In c++14, std::result_of is supposed to result in SFINAE if the expression is ill-formed*. Instead I'm getting a compilation error ("invalid operands to binary expression") on my final case below (i.e. letting the compiler deduce type for…
KentH
  • 1,204
  • 1
  • 14
  • 23
2
votes
1 answer

result_of of call to member function of template parameter

I need to get the result of a member function of a template parameter of a class. Unfortunately, I am bound to C++03 and cannot use decltype, but I can use tr1::result_of. I tried the following code, but that did not work with my compiler (gcc 4.3,…
Jens
  • 9,058
  • 2
  • 26
  • 43
2
votes
2 answers

std::result_of doesn't work on functor which operator() has rvalue argument in visual studio 2012

Here simple example: #include int foo() { return 2; } struct A { int operator()(int&& x) { return x*2; } }; int main(int, char**) { std::result_of::type x = 42; return 0; } When i try to…
2
votes
3 answers

Detecting function parameter type

I went about with the following code to detect the long argument to a given function. So, given: int f(int *) { return 0; } I want to extract int *. Here is my attempt: template struct SingleArg { typedef U…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
2
votes
1 answer

basic boost spirit semantic action doesn't compile

I am trying to add a greater than operator > to a ast: the code is 95% identical to the code in the docs. Two points of interest below A block of code where I'm trying to write support for greater than: commented in the code below. A single line in…
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
1
vote
0 answers

How C++ 11 std::result_of or C++ 17 invoke_result works

I'm trying to understand C++ metaprogramming and looking at a type_traits header. But I really can't understand, how std::result_of works. A simple version of result_of is not difficult: template struct result_of {}; template
edKotinsky
  • 11
  • 3
1
vote
1 answer

Why does operator () with type argument can be applied to type in the context of result_of?

As fas as I understand, result_of_t should be a type, that will be at the end of the evaluation of an expression. decltype(&foo) in the code below yields the type int (*)(int), but what does (int) outside of decltype? #include int…
P. Dmitry
  • 1,123
  • 8
  • 26
1
vote
2 answers

Use invoke_result with void argument type?

I'm trying to do the following: struct Unwrapper { template auto operator()(const T& arg, std::enable_if_t, void>* = nullptr) {return arg;} template auto operator()(const T& arg,…
haelix
  • 4,245
  • 4
  • 34
  • 56