Questions tagged [static-assert]

An assertion statement that is verified at the compilation time. A feature of C++11/C++14, supported by gcc since 4.3

Static assertion is written like

static_assert ( sizeof(int) >= 2 ,  "16 bit architecture not supported" ) ;

where the first parameter must be a constant expression, which should be possible to be evaluated at compile time. If the first expression is false, compiler raises an error with the message given in Second parameter. If expression is true compilation proceeds normally, without causing any impact to generated code.

Static assert was proposed because of forms of assertion are sufficient when working with templates. In GCC, supported since the version 4.3.

Earlier to static_assert, two forms were available to raise compile time errors:

  • #error pragma directive, which is not supported by all compilers. Either way, pre-processing is too early and hence not much usable.
  • Using tricks to prepare macros, so that compiler would raise errors. A compile time divide-by-zero, duplicated case in switch, zero or negative sized array were common to build compile time errors.

static_assert is also quite used in template based programming, where to class designer expects type of arguments to be of given size and types (for example, number of bits must be less than 64 for a Bit class).

A static_assert statement can be placed almost everywhere: globally, local to a function, in class declaration/definition, in class template etc. Thus making it highly useful.

465 questions
25
votes
1 answer

Is there a compile-time func/macro to determine if a C++0x struct is POD?

I'd like to have a C++0x static_assert that tests whether a given struct type is POD (to prevent other programmers from inadvertently breaking it with new members). ie, struct A // is a POD type { int x,y,z; } struct B // is not a POD type (has…
Crashworks
  • 40,496
  • 12
  • 101
  • 170
25
votes
4 answers

Clang and GCC vs MSVC and ICC: Is a static_assert in the copy/move constructor required to work, if copy/move elision could apply too?

I have a static_assert in a move constructor of a template struct of mine. Is this static_assert required to be considered by the compiler, even if copy elision is possible? This is the stripped-down scenario: #include…
Rumburak
  • 3,416
  • 16
  • 27
25
votes
1 answer

static_assert inside/outside class definition

Why does static_assert need to be out side of the class definition? Failing code #include class A { public: A(A&&) noexcept {} static_assert(std::is_nothrow_move_constructible::value, "ERROR"); }; int main() { } Working…
Parkesy
  • 285
  • 3
  • 7
24
votes
3 answers

Will consteval allow using static_assert on function arguments?

Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr. That makes sense because the compiler still has to create a non-constexpr instantiation of this function in case some…
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
24
votes
3 answers

static_assert if expressions is constexpr

I want to create a class template template class X { // here I'll use T::value (among other things) }; T::value will often be a constexpr static variable, but not always. T::value has to be positive value, so I want to let people know…
RiaD
  • 46,822
  • 11
  • 79
  • 123
23
votes
3 answers

Is there any way to slip a static_assert into an expression in ISO C++11?

In C++11 it is legal to write, for instance: int b = (some_function_returning_void(), 1020); And you'll get back 1020. But it won't let you write: int b = (static_assert(2 > 1, "all is lost"), 304); The documentation explains the legal spots…
23
votes
7 answers

How to combine static_assert with sizeof and stringify?

Memory usage is quite critical in my application. Therefore I have specific asserts that check for the memory size at compile time and give a static_assert if the size is different from what we considered correct before. I have defined a macro like…
Patrick
  • 23,217
  • 12
  • 67
  • 130
22
votes
3 answers

static_assert - a way to dynamically customize error message

Is there a way to make static_assert's string being dynamically customized and then displayed? What I mean is something like: //pseudo code static_assert(Check_Range::value, "Value of " + typeof(T) + " type is not so good ;)");
smallB
  • 16,662
  • 33
  • 107
  • 151
22
votes
3 answers

Enforce template type through static_assert

I'm trying to understand the usefulness of static_assert, and I want to know if it can help me in enforcing a design, and if so, how. I have a general template class that hides its own implementation inside another template class which is partially…
Zeenobit
  • 4,954
  • 3
  • 34
  • 46
22
votes
2 answers

std::cout equivalent at compile time, or static_assert stringification of compile-time constant values in c++11

Is there a way to print the value of a constexpr or #defined value at compile time? I want the equivalent of std::cout <<, or some way to do something like constexpr int PI_INT = 4; static_assert(PI_INT == 3, const_str_join("PI_INT…
Jason Gross
  • 5,928
  • 1
  • 26
  • 53
21
votes
4 answers

Can static_assert check if a type is a vector?

Can static_assert check if a type is a vector? IE, an int would raise the assertion, whereas a vector would not. I'm thinking of something along the lines of: static_assert(decltype(T) == std::vector, "Some error")
小太郎
  • 5,510
  • 6
  • 37
  • 48
21
votes
5 answers

How to print result of a compile-time calculation in C++?

I've wrote several constexpr functions and use them in static_asserts to control some resource limits. But I'd like to not only enforce compile-time predicate but also to see the actual values calculated during normal compilation process or at least…
Dmitry Vyal
  • 2,347
  • 2
  • 24
  • 24
21
votes
6 answers

Display integer at compile time in static_assert()

Here is a simplified version of what I'm trying to do enum First { a, b, c, nbElementFirstEnum, }; enum Second { a, b, c, nbElementSecondEnum, }; static_assert( First::nbElementFirstEnum ==…
b3nj1
  • 667
  • 1
  • 6
  • 17
20
votes
8 answers

Does GCC have a built-in compile time assert?

Our existing compile-time assert implementation is based on negative array index, and it provides poor diagnostic output on GCC. C++0x's static_assert is a very nice feature, and the diagnostic output it provides is much better. I know GCC has…
VladLosev
  • 7,296
  • 2
  • 35
  • 46
20
votes
2 answers

Why can't "is_base_of" be used inside a class declaration (incomplete type)?

I completely see why this cannot work: class Base {}; class A; static_assert(std::is_base_of::value, ""); Because there is no information about a 'class hierarchy', but... Why cannot the following work? class Base {}; class A : public Base…
user2561762
  • 415
  • 2
  • 11
1
2
3
30 31