c++ - constexpr if and static_assert -
p0292r1 constexpr if has been included, on track c++17. seems useful (and can replace use of sfinae), comment regarding static_assert
being ill-formed, no diagnostic required in false branch scares me:
disarming static_assert declarations in non-taken branch of constexpr if not proposed. void f() { if constexpr (false) static_assert(false); // ill-formed } template<class t> void g() { if constexpr (false) static_assert(false); // ill-formed; no // diagnostic required template definition }
i take it's forbidden use static_assert
inside constexpr if (at least false / non-taken branch, in practice means it's not safe or useful thing do).
how come standard text? find no mentioning of static_assert
in proposal wording, , c++14 constexpr functions allow static_assert
(details @ cppreference: constexpr).
is hiding in new sentence (after 6.4.1) ? :
when constexpr if statement appears in templated entity, during instantiation of enclosing template or generic lambda, discarded statement not instantiated.
from there on, assume forbidden, no diagnostic required, call other constexpr (template) functions somewhere down call graph may call static_assert
.
bottom line:
if understanding correct, doesn't put quite hard limit on safety , usefulness of constexpr if
have know (from documentation or code inspection) use of static_assert
? worries misplaced?
update:
this code compiles without warning (clang head 3.9.0) understanding ill-formed, no diagnostic required. valid or not?
template< typename t> constexpr void other_library_foo(){ static_assert(std::is_same<t,int>::value); } template<class t> void g() { if constexpr (false) other_library_foo<t>(); } int main(){ g<float>(); g<int>(); }
this talking well-established rule templates - same rule allows compilers diagnose template<class> void f() { return 1; }
. [temp.res]/8 new change bolded:
the program ill-formed, no diagnostic required, if:
- no valid specialization can generated template or substatement of
constexpr if
statement ([stmt.if]) within template , template not instantiated, or- [...]
no valid specialization can generated template containing static_assert
condition nondependent , evaluates false
, program ill-formed ndr.
static_assert
s dependent condition can evaluate true
@ least 1 type not affected.
Comments
Post a Comment