Namespaces
Variants
Views
Actions

Talk:cpp/language/static assert

From cppreference.com

ok they say:

"A static assert declaration may appear at namespace and block scope (as a block declaration) and inside a class body (as a member declaration) "

But on my GCC 5.1.0 it works also on global scope. Is the above text correct/complete? Or is it just a silent feature enabled on my gcc compiler.

I my own eyes static_assert would make most sense on global scope anyway.

212.238.237.61 10:10, 3 September 2017 (PDT)

Global scope is a kind of namespace scope. T. Canens (talk) 12:18, 3 September 2017 (PDT)


This page doesn't appear to mention that the behaviour changes based on whether it contains a dependent parameter or not. E.g. the following will always (or can? - maybe compiler-dependent) assert, even if the function is never instantiated:

 template <class T> static int Foo ()
 {
   static_assert (false);
 }

The following will only assert if the function is instantiated:

 template<typename> constexpr bool False = false;
 
 template <class T> static int Foo ()
 {
   static_assert (False<T>);
 }

Thanks to Jonathan Caves (MS) for pointing this out.

That's a templates quirk rather than a static_assert quirk. Note that both examples are equally ill-formed, just the second doesn't require a diagnostic [1] --Ybab321 (talk) 10:40, 20 February 2022 (PST)
we do mention this "dependent false" problem at cpp/language/if#Constexpr_if, which is where it often comes up in real life, but a similar note on static asserts inside templates could be worth it here, too. --Cubbi (talk) 11:18, 20 February 2022 (PST)

[edit] Should message be unevaluated-string (or something else)?

There is no message parameter (rather, unevaluated-string and constant-expression, yet the Notes section is referring to a message parameter. ticotico (talk)