当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

Here simple example:

#include <type_traits>

int foo() {

return 2;

}

struct A {

int operator()(int&& x) {

return x*2;

}

};

int main(int, char**) {

std::result_of<A&(int)>::type x = 42;

return 0;

}

When i try to compile it in VisualStudio 2012 I get an error:

error C2664: 'int A::operator ()(int &&)' : cannot convert parameter 1 from 'int' to 'int &&'

You cannot bind an lvalue to an rvalue reference

I compile same code in mingw-g++ and everything work fine.

Can I do anything rather than write my own result_of realization? (I wrote it as workaround).

网友答案:

I think this is a bug in the implementation of std::result_of or std::declval<>().

Paragraph 20.9.7.6 (Table 57) of the C++11 Standard specifies that the following must hold for result_of:

If the expression INVOKE(declval<Fn>(), declval<ArgTypes>()...) is well formed when treated as an unevaluated operand (Clause 5), the member typedef type shall name the type decltype(INVOKE(declval<Fn>(), declval<ArgTypes>()...)); otherwise, there shall be no member type.

In this case, INVOKE(declval<A&>(), declval<int>()) resolves to invoking an lvalue of type A with an rvalue of type int (see paragraph 20.8.2/1).

Considering the call operator of A, the member typedef type shall resolve to int.

网友答案:

Inheriting the functor from std::unary_function seems to resolve this bug of VS2012

分享给朋友:
您可能感兴趣的文章:
随机阅读: