C++ 错误:`class std::result_`中的`type`未命名类型
下面的示例在我尝试过的所有编译器中都失败了:gcc-8.2、clang-8.0(两个选项都尝试了C++ 错误:`class std::result_`中的`type`未命名类型,c++,templates,c++17,c++20,result-of,C++,Templates,C++17,C++20,Result Of,下面的示例在我尝试过的所有编译器中都失败了:gcc-8.2、clang-8.0(两个选项都尝试了--std=c++17和std=c++2a)和zapcc-2017.08 在我看来,代码示例是有效的,应该编译。或者,至少应该有一个更全面的错误。它看起来确实像std库中的一个bug,没有涵盖的result\u的这个特殊情况。我错了吗 #include <type_traits> using namespace std; struct bar { int a; long b
--std=c++17
和std=c++2a
)和zapcc-2017.08
在我看来,代码示例是有效的,应该编译。或者,至少应该有一个更全面的错误。它看起来确实像std库中的一个bug,没有涵盖的result\u的这个特殊情况。我错了吗
#include <type_traits>
using namespace std;
struct bar {
int a;
long b;
};
template<auto M>
struct foo {
static auto q(bar & b) {
return b.*M;
}
};
template<auto M>
auto qoo(bar & b) {
return b.*M;
}
// error: 'type' in 'class std::result_of<int(bar&)>' does not name a type
using f = typename result_of<decltype(foo<&bar::a>::q)>::type;
// error: 'type' in 'class std::result_of<int(bar&)>' does not name a type
using q= typename result_of<decltype(qoo<&bar::a>)>::type;
#包括
使用名称空间std;
结构条{
INTA;
长b;
};
模板
结构foo{
静态自动调q(巴和巴){
返回b*M;
}
};
模板
自动qoo(酒吧和早餐){
返回b*M;
}
//错误:“类std::result\u of”中的“type”未命名类型
使用f=typename result_of::type;
//错误:“类std::result\u of”中的“type”未命名类型
使用q=typename result\u of::type;
result\u of t
表示“使用参数调用/调用F
的结果…
”
result\u of t
表示“使用bar&
调用int
的结果”。它不存在,因为你不能用任何东西调用int
的结果\u不是“从函数类型中提取返回类型”。尝试
using f = typename std::result_of<decltype(&foo<&bar::a>::q)(bar&)>::type;
using q= typename std::result_of<decltype(&qoo<&bar::a>)(bar&)>::type;
您只将可调用的类型传递给
的std::result_(几乎:在foo
之前,您还需要一个&
);您还必须传递参数的类型(在本例中,只有一个参数:一个bar
reference),所以
std::结果
根据我的经验,result\u of
真的没有什么能做decltype
做不到的事情(或者有助于简化代码):
在本例中也是如此,其中decltype
,并将给出比的result\u更简单的结果:
using f = decltype(foo<&bar::a>::q(declval<bar&>()));
using q = decltype(qoo<&bar::a>(declval<bar&>()));
使用f=decltype(foo::q(declval());
使用q=decltype(qoo(declval());
请回答您的问题。@πάνταῥεῖ 您处于C++11模式,这使得它由于完全不同的原因而失败。将其置于C++17模式,您将获得与OP显示的相同的输出。@LightnessRacesinOrbit Ooops.@Lightness Races in Orbit-如果您想支持所有可调用对象,这里列出的五个result\u中没有一个是必需的。@T.C.我想不出一个不能使用declval
和decltype
实现,但我想理解。我应该问另一个问题,或者你可以在评论中给出一个简单的例子吗?全部invocable对象都包括指向成员的指针。@T.C.我不能使用declval
调用指向成员的指针吗?例如:当然。现在尝试编写一个既适用于成员指针又适用于函数对象的指针。
std::result_of<decltype(&foo<&bar::a>::q)(bar&)>
using f = decltype(foo<&bar::a>::q(declval<bar&>()));
using q = decltype(qoo<&bar::a>(declval<bar&>()));