c++ - Should static_assert be triggered with a typedef? -
i noticed static assertions in class templates not triggered when instantiations typedef'ed.
#include <type_traits> template <typename t> struct test_assert { static_assert( std::is_same< t, int >::value, "should fail" ); }; typedef test_assert< float > t; this code compiles without error. if try create instance, assertion fails:
t obj; // error: static assertion failed: "should fail" finally, if replace condition false, assertion fails if don't instantiate class template:
template <typename t> struct test_assert { static_assert( false, "always fails" ); }; i tried code on gcc-4.5.1 , gcc-4.7.0. behavior normal? @ time compiler supposed verify static assertions? guess two-phase lookup involved, shouldn't typedef trigger second phase?
i tried code on gcc-4.5.1 , gcc-4.7.0. behavior normal?
yes
at time compiler supposed verify static assertions?
this interesting question. during instantiation, first phase lookup non-dependent names , second lookup phase asserts depend on template arguments.
guess two-phase lookup involved, shouldn't typedef trigger second phase?
templates compiled on demand, typedef creates alias template , not trigger instantiation. consider following code:
template <typename t> class unique_ptr; typedef unique_ptr<int> int_unique_ptr; the template declared, suffices typedef, generates alias type. on other side, if create object of type, template must instantiated (again on demand, member functions not instantiated).
Comments
Post a Comment