2017-04-21 88 views
0

我需要一個類型trait,確定一個類是否是給定模板的專業化。 This answer提供了一個適用於大多數情況下的實現。is_specialization類型trait爲靜態constexpr成員

但是,它似乎不適用於靜態constexpr成員類型。在下面的例子中(也wandbox可用),最後static_assert上鏘和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成員的類型?

回答

0

我剛剛發現,如果你衰減靜態constexpr成員的類型去除cv-qualifiers,那麼這個工作就可以發揮作用。

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