【问题标题】:C++11 is_same type trait for templates模板的 C++11 is_same 类型特征
【发布时间】:2023-04-04 18:57:01
【问题描述】:

是否可以检查类型T 是任意类型和大小的std::array?

我可以检查一个特定的数组,例如:

    is_same<T, std::array<int,5>>::value

但我想检查T 是std::array 的任何实例。如下所示(当然,它不会编译):

    is_same<T, std::array>::value

有没有办法实现这一点(可能不使用is_same)?

【问题讨论】:

  • 你可以写一个通用的 is_same_template 并说 is_same_template &lt;T::template array, std::array&gt; 。编译器必须支持 c++11 注入的类名规则。

标签: c++ templates c++11 metaprogramming typetraits


【解决方案1】:

你必须自己写,但很简单:

template<typename>
struct is_std_array : std::false_type {};

template<typename T, std::size_t N>
struct is_std_array<std::array<T,N>> : std::true_type {};

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多