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
20
votes
1 answer

Why does std::make_pair not return a pair? Or does it?

My internal sanity check failed so I'm rerunning it on Stackoverflow. The following code: #include #include #include int main() { constexpr auto pair_of_ints = std::make_pair(1, 2); std::cerr <<…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
19
votes
3 answers

How to use std::is_volatile?

I am trying to disallow a specific operation on volatile types. To accomplish this I am trying to use std::is_volatile, but the code below is compiling without errors, which is not what I want. Why is is_volatile::value false in the case…
MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59
19
votes
3 answers

Static assertions about the size of a template struct

I need to make sure a template struct is exactly the size of its members. static_assert seems to be the tool of choice here. However, I cannot use a static_assert inside the struct itself, because the size is not yet known there. Here is what I…
gexicide
  • 38,535
  • 21
  • 92
  • 152
19
votes
3 answers

How do I check if a template parameter is a power of two?

I want to create a structure that allocates statically an array of 2^N bytes, but I don't want the users of this structure to specify this size as the exponent. Example: my_stupid_array a1; // I want this! my_stupid_array a2; //…
ivarec
  • 2,542
  • 2
  • 34
  • 57
18
votes
1 answer

C++ concepts vs static_assert

What is exactly new in c++ concepts? In my understanding they are functionally equal to using static_assert, but in a 'nice' manner meaning that compiler errors will be more readable (as Bjarne Stroustup said you won't get 10 pages or erros, but…
17
votes
2 answers

Static Assert for Public Inheritance

I built a helper class that would construct a custom class via templates, this custom class must inherit from a certain class, I can check for this with std::is_base_of. However I also need to check that the inheritance is public, how can this be…
Thomas Monkman
  • 335
  • 2
  • 10
17
votes
7 answers

How do you static_assert the values in a parameter pack of a variadic template?

I'm creating a variadic template. Let's say I have something like this: template class Sequence final { // Unpack parameter pack into a constexpr array constexpr static T count = sizeof...(Numbers); …
Venemo
  • 18,515
  • 13
  • 84
  • 125
16
votes
1 answer

Compile time check AND runtime check 'at the same time'

Suppose I have the following simplified program: Link to godbolt.org: #include struct Dimensions { Dimensions& operator=(int i) { assert(i != 0); return *this; } }; int getDim(); int main() { Dimensions dims; …
darune
  • 10,480
  • 2
  • 24
  • 62
15
votes
2 answers

How to static_assert the size of a std::array member

I would like to be explicit about array size restrictions on a member variable, to stop others from accidentally making silly changes. The following naive attempt will not compile: struct Foo { std::array< int, 1024 > some_array; …
paddy
  • 60,864
  • 6
  • 61
  • 103
15
votes
2 answers

Code I've never seen in C++11

I'm looking at this source code template struct conv2bin; template struct conv2bin { static_assert(high == '0' || high == '1', "no bin num!"); static int const value = (high - '0')…
14
votes
2 answers

GCC 4.9 gives error when checking inline function's pointer (with static_assert)

Consider the following case typedef void (*foo)(); template struct bar { static_assert(f!=nullptr,"f == null!"); }; void baz() {} inline void bax() { } bar ok; bar bad; // error: non-constant condition for static…
hutorny
  • 863
  • 11
  • 17
14
votes
3 answers

constexpr, static_assert, and inlining

I previously asked about function overloading based on whether the arguments are constexpr. I'm trying to work around the disappointing answer to that question to make a smarter assert function. This is roughly what I am trying to do: inline void…
David Stone
  • 26,872
  • 14
  • 68
  • 84
13
votes
3 answers

In C++0x is there something like static_assert which gives a warning instead of an error?

I would like to do this for usages which may be inefficient but not necessarily incorrect.
Clinton
  • 22,361
  • 15
  • 67
  • 163
13
votes
2 answers

if constexpr with static_assert in lambda, which compiler is correct?

When we want to use a static_assert in a if constexpr we must make the condition dependent on some template parameter. Interestingly, gcc and clang disagree when the code is wrapped in a lambda. The following code compiles with gcc, but clang…
florestan
  • 4,405
  • 2
  • 14
  • 28
13
votes
2 answers

How to static assert in a member function only if it is used?

I have the following scheme: struct Baz {}; struct Qux {}; struct Base { virtual ~Base() {} virtual void foo() = 0; }; template struct Identity { static bool const value = false; }; template void bar(T) {…
101010
  • 41,839
  • 11
  • 94
  • 168
1 2
3
30 31