gpt4 book ai didi

c++ - std::result_of 不适用于 visual studio 2012 中 operator() 具有右值参数的仿函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:24 26 4
gpt4 key购买 nike

这里是一个简单的例子:

#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;
}

当我尝试在 VisualStudio 2012 中编译它时出现错误:

error C2664: 'int A::operator ()(int &&)' : cannot convert parameter 1 from 'int' to 'int &&'
You cannot bind an lvalue to an rvalue reference

我在 mingw-g++ 中编译相同的代码,一切正常。我可以做任何事情而不是写我自己的 result_of 实现吗? (我把它写成解决方法)。

最佳答案

我认为这是 std::result_of 实现中的错误或 std::declval<>() .

C++11 标准第 20.9.7.6 段(表 57)指定以下内容必须适用于 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.

在这种情况下,INVOKE(declval<A&>(), declval<int>())解析为调用 A 类型的左值具有 int 类型的右值(参见第 20.8.2/1 段)。

考虑 A 的调用运营商, 成员类型定义 type应解决int .

关于c++ - std::result_of 不适用于 visual studio 2012 中 operator() 具有右值参数的仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16839624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com