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
112
votes
2 answers

Difference between std::result_of and decltype

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
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
33
votes
4 answers

What is the reason for `std::result_of` deprecated in C++17?

I saw std::result_of is being deprecated in C++17. What is the reason for std::result_of deprecated in C++17? Also I would like to know the difference between std::result_of and std::invoke_result.
msc
  • 33,420
  • 29
  • 119
  • 214
30
votes
8 answers

Metaprograming: Failure of Function Definition Defines a Separate Function

In this answer I define a template based on the type's is_arithmetic property: template enable_if_t::value, string> stringify(T t){ return to_string(t); } template enable_if_t::value,…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
27
votes
1 answer

std::result_of simple function

#include #include double f(int i) { return i+0.1; } struct F { public: double operator ()(int i) { return i+0.1; } }; int main(int, char**) { std::result_of::type x; // ok …
user1494506
  • 1,283
  • 1
  • 12
  • 9
20
votes
2 answers

result_of for member object with cv-qualified argument

Given the following declarations: struct MyClass { }; typedef int MyClass::*Mp; On both gcc 6.2 and Clang compiler I have tried, result_of::type yields int&&. Summary of my question: Why int&& and not either const int&& or…
Pablo Halpern
  • 941
  • 7
  • 12
17
votes
1 answer

Can I write a function type that returns a function?

The following fails to compile on both gcc and clang #include int foo(); int main() { using R = std::result_of_t; // error } The error on both compilers deals with the illegality of declaring a function…
Barry
  • 286,269
  • 29
  • 621
  • 977
12
votes
2 answers

C++ Thread taking reference argument failed compile

#include #include using namespace std; void f1(double& ret) { ret=5.; } int main() { double ret=0.; thread t1(f1, ret); t1.join(); cout << "ret=" << ret << endl; } The above code fails compilation with the…
Rich
  • 1,669
  • 2
  • 19
  • 32
11
votes
1 answer

Correct way of using invoke_result?

On cppreference, it is written that the correct way of using std::result_of is: template std::result_of_t // instead of std::result_of_t, which is wrong my_invoke(F&& f, Args&&... args) { …
Vincent
  • 57,703
  • 61
  • 205
  • 388
9
votes
1 answer

result_of does not define type for mem_fn

I have the following piece of code: #include struct X { int get() const& { return 42; } }; template std::result_of_t Apply(Func fn) { X x; return fn(x); } int main(void) { …
phimuemue
  • 34,669
  • 9
  • 84
  • 115
6
votes
1 answer

Deduce return type of member function

In a template function I was trying to create a std::vector with its value_type being dependent on a member function of the template parameter to the function. This template parameter is restricted to being a vector containing unique pointers of a…
KorbenDose
  • 797
  • 1
  • 7
  • 23
6
votes
1 answer

c++ error std::result_of does not name a type

After g++ -std=c++0x'ing std::result_of produces the following error message error: ‘result_of’ in namespace ‘std’ does not name a type (g++ version 4.5.0 on SUSE.) The relevant piece of code, sufficient for reproducing the error is below #include…
user1672572
  • 321
  • 2
  • 9
5
votes
1 answer

Using std::result_of with an overloaded method

I added an overloaded method to an existing class, which now causes a compilation error in our unit tests. I have replicated the issue with the following code: #include #include class Foo { public: Foo() {}; int…
Class Skeleton
  • 2,913
  • 6
  • 31
  • 51
4
votes
1 answer

result_of doesn't work for me

#include using namespace std; struct asd{ void f(); }; int f(); typedef typename result_of::type result_free; typedef typename result_of::type result_mem; both the typedefs give an error In file…
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
4
votes
1 answer

C++11: Why result_of can accept functor type as lvalue_reference, but not function type as lvalue_reference?

I've got program below: #include #include using namespace std; template ::type> R call(F& f) { return f(); } struct S { double operator()(){return 0.0;} }; int f(){return 1;} int…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
4
votes
1 answer

c++ deduction of "non type pointer to function" class template parameters

Consider a template class like: template class Proxy { void run() { ReturnType ret = Fn(); // ... do something ... } }; // and a functions int fn1() { return 5; } float fn2() {…
Hrvoje Prgeša
  • 2,051
  • 5
  • 21
  • 36
1
2 3 4