我需要一个类型特征来确定一个类是否是给定模板的特殊化。This answer提供了一个在大多数情况下都可以工作的实现。
但是,它似乎不适用于静态常量表达式成员类型。在以下示例(也可在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>{});
}我有两个问题:这是正确的行为吗?当我引用静态常量成员的类型时,我如何编写类型特征?
发布于 2017-04-21 23:29:10
我刚刚发现,如果您衰减静态常量表达式成员的类型以剥离cv限定符,那么它就可以工作。
static_assert(is_specialization<std::decay_t<decltype(foo::z)>, bar>{});https://stackoverflow.com/questions/43546558
复制相似问题