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
149
votes
9 answers

What does static_assert do, and what would you use it for?

Could you give an example where static_assert(...) ('C++11') would solve the problem in hand elegantly? I am familiar with run-time assert(...). When should I prefer static_assert(...) over regular assert(...)? Also, in boost there is something…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
125
votes
16 answers

Static assert in C

How can compile-time static asserts be implemented in C (not C++), with particular emphasis on GCC?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
69
votes
7 answers

constexpr if and static_assert

P0292R1 constexpr if has been included, on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill-formed, no diagnostic required in the false branch scares me: Disarming static_assert…
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
68
votes
4 answers

Integrate type name in static_assert output?

I like to give helpful errors / messages, and I also want to do so for my static_asserts. The problem is, that they depend on template parameters. Normally, those parameters will get displayed on way or an other due to the error raised, but they are…
Xeo
  • 129,499
  • 52
  • 291
  • 397
61
votes
2 answers

C++11 - static_assert within constexpr function?

How would one properly do a static_assert within a constexpr function? For example: constexpr int do_something(int x) { static_assert(x > 0, "x must be > 0"); return x + 5; } This is not valid C++11 code, because a constexpr function must only…
RétroX
  • 1,996
  • 4
  • 16
  • 24
49
votes
4 answers

static_assert fails compilation even though template function is called nowhere

I use g++ 4.6.3, (currently default package for ubuntu 12.04) with the flag c++0x, and I stumble across this: template inline T getValue(AnObject&) { static_assert(false , "this function has to be implemented for desired…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
45
votes
5 answers

How to check the size of a structure at compile time?

I want to add code that during compilation checks the size of a structure to make sure that it is a predefined size. For example I want to make sure that the size of this structure is 1024 byte when I am porting this code or when I am…
mans
  • 17,104
  • 45
  • 172
  • 321
36
votes
4 answers

How do I restrict a template class to certain built-in types?

This issue has been discussed a few times but all the solutions I have found either didn't work or were based on boost's static assert. My problem is simple. I have a class, and I only want to allow real types (double and float). I want a…
quant
  • 21,507
  • 32
  • 115
  • 211
35
votes
5 answers

Use static_assert to check types passed to macro

I unfortunately have several macros left over from the original version of my library that employed some pretty crazy C. In particular, I have a series of macros that expect certain types to be passed to them. Is it possible to do something along…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
30
votes
1 answer

How to check if two types are same at compiletime(bonus points if it works with Boost strong typedef)

I was wondering if it is possible to check if 2 types are same at compile time. What I came up with is(idk if it works because it feels hackish and IDK standard that good so IDK what to look for when testing). #include…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
29
votes
4 answers

When to use `static_assert` instead of SFINAE?

I have been using (and seen used) static_assert to flag undesired values of template parameter values. However, for all cases I came across it seems better and more elegant to disable those undesired values via SFINAE. For example: template
Walter
  • 44,150
  • 20
  • 113
  • 196
28
votes
2 answers

Why can't a const mutable lambda with an auto& parameter be invoked?

#include int main() { auto f1 = [](auto&) mutable {}; static_assert(std::is_invocable_v); // ok auto const f2 = [](auto&) {}; static_assert(std::is_invocable_v); // ok auto…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
27
votes
1 answer

How to write runnable tests of static_assert?

I am writing a unit-test suite for a source code library that contains static_asserts. I want to provide assurance these static_asserts do no more and no less than they are desired to do, in design terms. So I would like to be able to test them. I…
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
26
votes
6 answers

BOOST_STATIC_ASSERT without boost

Since boost is forbidden in a company I work for I need to implement its functionality in pure C++. I've looked into boost sources but they seem to be too complex to understand, at least for me. I know there is something called static_assert() in…
Konstantin
  • 6,061
  • 8
  • 40
  • 48
26
votes
11 answers

Ways to ASSERT expressions at build time in C

I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values…
tonylo
  • 3,311
  • 3
  • 28
  • 27
1
2 3
30 31