Is it possible to define a data item without [specializing] the entire template?
static
template data members are allowed to be explicitly specialized ([temp.expl.spec]), however, if you want to do this, you can no longer specify the initializer for the member in the class template (class.static.data). I.e
constexpr
const
, :
template<int>
struct X {
static const int x;
};
template<int I>
const int X<I>::x = 0;
template<>
const int X<1>::x = 1;
:
template<int>
struct X {
static const int x = 0;
};
template<>
const int X<1>::x = 1;
, , .
, const
constexpr
, , (class.static.data):
A static
literal constexpr
; , - -, -,
, , static
, constexpr
, constexpr
. IMHO .
, , .
gcc 8.0.0 ( ) (), , ().
clang 6.0.0 as-is (), , (, , , , )
MSVC 19.00.23506 (), , ( ) ().
, Traits:
template<int>
struct X_Traits{
static constexpr int value = 0;
};
template<>
struct X_Traits<1>{
static constexpr int value = 1;
};
template<int I>
struct X {
static constexpr int x=X_Traits<I>::value;
};
++ 17 constexpr, , :
template<int I>
struct X_Traits{
static constexpr int get_value(){
if constexpr(I==1){
return 1;
}else{
return 0;
}
}
};
template<int I>
struct X {
static constexpr int x=X_Traits<I>::get_value();
};
int main(){
static_assert(X<0>::x == 0);
static_assert(X<1>::x == 1);
}