Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:`class std::result_`中的`type`未命名类型_C++_Templates_C++17_C++20_Result Of - Fatal编程技术网

C++ 错误:`class std::result_`中的`type`未命名类型

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

下面的示例在我尝试过的所有编译器中都失败了: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;
};

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&>()));