【问题标题】:What compiler option/library do I need to use detect_or_t type trait?我需要什么编译器选项/库才能使用 detect_or_t 类型特征?
【发布时间】:2016-04-05 06:21:34
【问题描述】:

我正在尝试使用来自<experimental/type_traits>std::experimental::detect_or_t

我需要什么编译器、选项、版本或库来编译来自http://en.cppreference.com/w/cpp/experimental/is_detected 的以下示例?

#include <experimental/type_traits>
#include <cstddef>

template<class T>
using diff_t = typename T::difference_type;

template <class Ptr>
using difference_type = std::experimental::detected_or_t<std::ptrdiff_t, diff_t, Ptr>;

struct Meow { using difference_type = int; };
struct Purr {};

int main()
{
    static_assert(std::is_same<difference_type<Meow>, int>::value, "Meow's difference_type should be int!");
    static_assert(std::is_same<difference_type<Purr>, std::ptrdiff_t>::value, "Purr's difference_type should be ptrdiff_t!");
}

我尝试使用clang++ -std=c++14g++ -std=c++14。还有-std=c++1y-std=c++17。我总是这样:

main.cpp:8:44: error: 'detected_or_t' in namespace 'std::experimental' does not name a template type

【问题讨论】:

  • 似乎适用于 GCC 6。在 gcc.godbolt.org 上尝试一下
  • @melak47,已验证godbolt.org/g/1tOlXq
  • 您可以在 Github 上为 libc++libstdc++ 跟踪此内容。后者支持,但前者尚不支持。
  • 适用于 g++ 6.3.1 -std=c++14 (c++14 is the default anyway)clang++ 3.8.1 -std=c++14

标签: g++ c++14 clang++ typetraits


【解决方案1】:

这些特征首先在 GCC 6.1.0 中添加到 libstdc++,如 GCC 6 release notes 中所述:

  • 对 Library Fundamentals TS 第二版的大多数功能的实验性支持。

以及手册中的实施状态表,位于 https://gcc.gnu.org/onlinedocs/gcc-6.1.0/libstdc++/manual/manual/status.html#table.cxx1z_ts_status

我对 libc++ 不太确定,但是 Clang 3.9.1 中的版本不支持它们,但当前的主干支持它们,所以我认为它们首先出现在 Clang 4.0.0 中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 2013-10-04
    • 2012-08-19
    相关资源
    最近更新 更多