result-of

Can I write a function type that returns a function?

為{幸葍}努か 提交于 2019-12-04 16:07:04
问题 The following fails to compile on both gcc and clang #include <type_traits> int foo(); int main() { using R = std::result_of_t<decltype(foo)()>; // error } The error on both compilers deals with the illegality of declaring a function returning a function. But I'm not declaring such a function - I'm just trying to write its type - since that's what result_of expects. Is this really still ill-formed? 回答1: You're passing a type-id , which is defined in [dcl.name] as […] syntactically a

std::result_of simple function

…衆ロ難τιáo~ 提交于 2019-11-30 01:49:31
#include <iostream> #include <type_traits> 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<F(int)>::type x; // ok // std::result_of<f(int)>::type x; // error: template argument 1 is invalid x = 0.1; std::cerr << x << std::endl; } Please explain why std::result_of<f(int)>::type x; is invalid... cppreference says "( std::result_of ) Deduces the return type of a function call expression at compile type.". what's the problem? std::result_of<T> requires T to be a type --but not just any type. T must be a

C++ Thread taking reference argument failed compile

ⅰ亾dé卋堺 提交于 2019-11-29 02:15:49
#include<iostream> #include<thread> 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 following error message : g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out In file included from /usr/local/include/c++/5.3.0/thread:39:0, from main.cpp:2: /usr/local/include/c++/5.3.0/functional: In instantiation of 'struct std::_Bind_simple<void (*(double))(double&)>': /usr/local/include/c++/5.3.0/thread:137:59: required from 'std::thread::thread(_Callable&

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

冷暖自知 提交于 2019-11-29 01:47:40
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 . T.C. already provided the obvious link, but perhaps the most horrific reason bears repeating: result_of involved forming the type F(Arg1, Arg2, ...) not for a function of those types returning F but for a function of type F accepting those types. (After all, the return type is the result of, well, result_of , not the input!) Aside from the adjustments associated with forming the function type, the

std::result_of simple function

前提是你 提交于 2019-11-28 22:38:01
问题 #include <iostream> #include <type_traits> 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<F(int)>::type x; // ok // std::result_of<f(int)>::type x; // error: template argument 1 is invalid x = 0.1; std::cerr << x << std::endl; } Please explain why std::result_of<f(int)>::type x; is invalid... cppreference says "( std::result_of ) Deduces the return type of a function call expression at compile type.".

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

天大地大妈咪最大 提交于 2019-11-27 16:01:40
问题 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 . 回答1: T.C. already provided the obvious link, but perhaps the most horrific reason bears repeating: result_of involved forming the type F(Arg1, Arg2, ...) not for a function of those types returning F but for a function of type F accepting those types. (After all, the return type is the result of,

C++ Thread taking reference argument failed compile

前提是你 提交于 2019-11-26 23:11:33
问题 #include<iostream> #include<thread> 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 following error message: g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out In file included from /usr/local/include/c++/5.3.0/thread:39:0, from main.cpp:2: /usr/local/include/c++/5.3.0/functional: In instantiation of 'struct std::_Bind_simple<void (*(double))

Metaprograming: Failure of Function Definition Defines a Separate Function

强颜欢笑 提交于 2019-11-26 11:23:42
In this answer I define a template based on the type's is_arithmetic property: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp suggests that rather than the is_arithmetic property of the type, that whether to_string is defined for the type be the template selection criteria. This is clearly desirable, but I don't know a way to say: If std::to_string is not defined then use the

Difference between std::result_of and decltype

匆匆过客 提交于 2019-11-26 10:10:00
问题 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)>::type 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 {

Metaprograming: Failure of Function Definition Defines a Separate Function

混江龙づ霸主 提交于 2019-11-26 02:24:53
问题 In this answer I define a template based on the type\'s is_arithmetic property: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp suggests that rather than the is_arithmetic property of the type, that whether to_string is defined for the type be the template selection criteria. This is