【问题标题】:is_specialization type trait for a static constexpr member静态 constexpr 成员的 is_specialization 类型特征
【发布时间】:2017-04-21 15:26:54
【问题描述】:

我需要一个类型特征来确定一个类是否是给定模板的特化。 This answer 提供了在大多数情况下都有效的实现。

但是,它似乎不适用于静态 constexpr 成员类型。在以下示例中(也可在 wandbox 上找到),最后一个 static_assert 在 Clang 和 GCC 主干上失败:

#include <type_traits>

// from https://stackoverflow.com/questions/16337610/how-to-know-if-a-type-is-a-specialization-of-stdvector
template<typename Test, template<typename...> class Ref>
struct is_specialization : std::false_type {};

template<template<typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref>: std::true_type {};

template<typename T>
struct bar {
  bool x;
};

struct foo {
  bar<int> y;
  static constexpr bar<bool> z{true};
};

int main() {
  static_assert(is_specialization<decltype(foo::y), bar>{});
  static_assert(is_specialization<decltype(foo::z), bar>{});
}

我有两个问题:这是正确的行为吗?当我引用静态 constexpr 成员的类型时,如何编写一个可以工作的类型特征?

【问题讨论】:

    标签: c++ metaprogramming template-specialization typetraits


    【解决方案1】:

    我刚刚发现,如果你衰减静态 constexpr 成员的类型以去除 cv 限定符,那么它会起作用。

    static_assert(is_specialization<std::decay_t<decltype(foo::z)>, bar>{});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多