c++ - static_assert in not initialized template class with valid specialization -
my question is, whether following code valid:
template<int i> class class { static_assert(sizeof(i) == 0, "class instantiated != 1"); }; template<> class class<1> {}; this snippet compiles g++. clang++ trapped static_assert:
error: static_assert failed "class instantiated non-int type" a template uses type instead of int like
template<typename t> class class { static_assert(sizeof(t) == 0, "class instantiated non-int type"); }; template<> class class<int> {}; is accepted both compilers. same pattern applies function templates.
i found open-std.org::non-dependent static_assert-declarations, not seem apply, because static_assert dependent on template parameter.
you may check described behavior on godbolt.org
edit: johan lundberg points out in comment question wrong. indeed sizeof(i) not depend on template parameter. r.sahu right: make more sense assert i != 1. again both compilers accept code.
however, still upper example compiles g++. open-std.org::non-dependent static_assert-declarations applies case (i apologize again wrong question in respect): g++ wrong in compiling code without error?
clang++ right reject code, g++ not wrong fail catch error; "no diagnostic required" situation.
the standard strictly defines expressions within template "type-dependent" and/or "value-dependent". given template<int i>, i value-dependent not type-dependent.
[14.6.2.2/4]: expressions of following forms never type-dependent (because type of expression cannot dependent):
- ...
sizeofunary-expression- ...
[14.6.2.3/2]: expressions of following form value-dependent if unary-expression or expression type-dependent or type-id dependent:
sizeofunary-expression- ...
so sizeof(i) not dependent.
finally, 14.6/8 says:
if hypothetical instantiation of template following definition ill-formed due construct not depend on template parameter, program ill-formed; no diagnostic required.
Comments
Post a Comment