Reputation: 806
I need a type trait that determines if a class is a specialization of a given template. This answer provides an implementation that works in most cases.
However, it does not seem to work for static constexpr member types. In the following example (also available on wandbox), the last static_assert fails on Clang and GCC trunk:
#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>{});
}
I have two questions: is this correct behavior, and how can I write a type trait that will work when I refer to the type of a static constexpr member?
Upvotes: 2
Views: 1611
Reputation: 806
I just discovered that works if you decay the type of the static constexpr member to strip the cv-qualifiers.
static_assert(is_specialization<std::decay_t<decltype(foo::z)>, bar>{});
Upvotes: 1