【发布时间】: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