gcc 6.1 std :: result_of编译错误

gcc 6.1 std::result_of compilation error

本文关键字:of 编译 错误 result std gcc      更新时间:2023-10-16

考虑一个小的独立用例

#include <type_traits>
namespace {
struct foo {
    template<class T, std::size_t = sizeof(T)>
    std::false_type  operator()(T&);
    std::true_type operator()(...);
};
struct FooIncomplete;
}

int main() {
    std::result_of<foo(FooIncomplete&)>::type();
    return 0;
}

使用--std=c++11标志使用gcc 4.9.3罚款。但是,使用gcc 6.1--std=c++11,它会产生一个汇编错误作为

main.cpp: In function 'int main()':
main.cpp:17:5: error: 'type' is not a member of 'std::result_of<{anonymous}::foo({anonymous}::FooIncomplete&)>'
     std::result_of<foo(FooIncomplete&)>::type();

我在这里想念什么?可能有什么可能的工作?

,因为C 14 result_of ::如果T不可呼应,则不存在。

在您的情况下,struct fooincomplete无需致电。

使用C 20的is_detected

namespace details {
  template<template<class...>class Z, class, class...Ts>
struct can_apply:std::false_type{};
  template<class...>struct voider{using type=void;};
  template<class...Ts>using void_t = typename voider<Ts...>::type;
  template<template<class...>class Z, class...Ts>
  struct can_apply<Z, void_t<Z<Ts...>>, Ts...>:std::true_type{};
}
template<template<class...>class Z, class...Ts>
using can_apply=typename details::can_apply<Z,void,Ts...>::type;
template<class T>
using size_of = std::integral_constant<std::size_t, sizeof(T)>;
template<class T>
using is_complete = can_apply< size_of, T >;

我们得到一个特质is_complete,如果FF,则可以将sizeof应用于T

请小心,因为与大多数功能不同,类型的完整性可以在编译单元之间甚至在同一单元中的不同位置之间发生变化。当类型some_template<some_args...>在程序中的不同点上更改时,C 不喜欢

实时示例。

相关文章: