result-of

C++11 result_of deducing my function type failed

て烟熏妆下的殇ゞ 提交于 2020-01-16 04:54:26
问题 I was trying a program below: #include<type_traits> using namespace std; template <class F, class R = typename result_of<F()>::type> R call(F& f) { return f(); } int answer() { return 42; } int main() { call(answer); return 0; } "call(answer)" fails to compile VC says 'R call(F&)' could not deduce template argument for 'R' GCC says |note: template argument deduction/substitution failed:|error: function returning a function I'm not sure if a "function name" could be used for templates. Where

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

血红的双手。 提交于 2019-12-23 12:01:32
问题 Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run() { ReturnType ret = Fn(); // ... do something ... } }; // and a functions int fn1() { return 5; } float fn2() { return 5; } This can be instantiated by using: Proxy<int, &fn1> p1; But explicitly declaring the return value type seems needless. What I am trying to achieve is something like: someProxyInstantation<&fn1> p1; someProxyInstantation<&fn2> p2; Unfortunately, I'm no c++ expect and

Can I get the Return Type of a Function From a Signature?

≯℡__Kan透↙ 提交于 2019-12-13 03:48:51
问题 So I have a ton of functions similar to these: template <typename T> bool Zero(const T, const T, const T); template <typename T> T One(const T, const T, const T, bool); template <typename T> T Three(const T, const T, const T, const T, const T, const T); For each of these functions I have a wrapper which uses the return type of these functions so it looks something like this: template <typename T> decltype(Zero<decltype(declval<T>().x)>(decltype(declval<decltype(declval<T>().x)>()), decltype

basic boost spirit semantic action doesn't compile

余生长醉 提交于 2019-12-12 09:04:42
问题 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 the parse for term which fails to compile because I don't yet understand semantic actions yet: not sure how to bind the lhs of lhs > rhs through phoenix and semantic actions. The solution should be trivial for regular users of Spirit, but I am

How Can I Use result_of Instead of decltype?

故事扮演 提交于 2019-12-11 03:46:51
问题 In this answer I create a type trait: template<typename T> using to_string_t = decltype(to_string(declval<T>())); 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 to replace the line above with something like this: template<typename T> using to_string_t = result_of<to_string(T)>; But I get a compiler error along the lines of: error C2275: 'T': illegal use of this type as an expression note: see declaration

Metaprograming: Failure of Function Definition Defines a Separate Function

大憨熊 提交于 2019-12-10 18:07:39
问题 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

Getting the Return Type of a Templatized Object's Method

我的未来我决定 提交于 2019-12-10 08:37:23
问题 Say that I have: template <typename T> struct Foo { T& func(); }; And I implement a Foo : Foo<int> 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 to just be able to do result_of_t<foo.func> and be done with it but I imagine it's significantly more difficult? How should I go about getting this return type? EDIT: I was hoping to accomplish this without without respect to how bar was declared.

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

瘦欲@ 提交于 2019-12-07 19:43:03
问题 This is my trick: template<typename F, typename TArg> auto get_return_value(F * f = NULL, TArg * arg = NULL) -> decltype((*f)(*arg)); Example of using: template<typename F, typename T> decltype(get_return_value<F,T>()) applyFtoT(F f, T t) { return f(t); } In case F is lambda: int b = applyFtoT([](int a){return a*2}, 10); // b == 20 Function get_return_value looks ugly i think... How to simplify it? 回答1: It seems like you could eliminate the need for get_return_value by changing the

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

≯℡__Kan透↙ 提交于 2019-12-06 03:50:46
This is my trick: template<typename F, typename TArg> auto get_return_value(F * f = NULL, TArg * arg = NULL) -> decltype((*f)(*arg)); Example of using: template<typename F, typename T> decltype(get_return_value<F,T>()) applyFtoT(F f, T t) { return f(t); } In case F is lambda: int b = applyFtoT([](int a){return a*2}, 10); // b == 20 Function get_return_value looks ugly i think... How to simplify it? It seems like you could eliminate the need for get_return_value by changing the declaration of applyFtoT like so: template<typename F, typename T> auto applyFtoT(F f, T t) -> decltype(f(t)) { return

basic boost spirit semantic action doesn't compile

淺唱寂寞╮ 提交于 2019-12-04 16:58:07
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 the parse for term which fails to compile because I don't yet understand semantic actions yet: not sure how to bind the lhs of lhs > rhs through phoenix and semantic actions. The solution should be trivial for regular users of Spirit, but I am still learning, and only getting by through examples so far. Any help would be appreciated. TIA. Code