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
0
votes
2 answers

Why do C11 global and local static asserts behave differently?

Consider the following code: const int g_foo = 1; // (1): _Static_assert(g_foo > 0, "g_foo > 0"); // error: expression in static assertion is not constant. _Static_assert(g_foo > 2, "g_foo > 2"); // error: expression in static assertion is not…
user2465116
  • 388
  • 2
  • 10
0
votes
2 answers

How to fold and static_assert all parameters?

The following doesn't compile: template void check_format(Args&&... args) { static_assert((true && std::is_fundamental::value)...); }
darune
  • 10,480
  • 2
  • 24
  • 62
0
votes
2 answers

static_assert : a certain function in Derive "must" hides Base's class function

I face a strange rare problem that I want to hide a Base class's function B::f1(int). class B{ public: void f1(int){} public: void f1(float){} }; class C : public B{ public: void f1(int){ …
javaLover
  • 6,347
  • 2
  • 22
  • 67
0
votes
1 answer

Statically asserting the size of a std::array whose type is obtained using decltype from the return value of a member function

I want to write a free function that can automatically determine the type of its parameter, based on the return value of a member function of a class. Using decltype, that part is easy. I also want to have a compile-time assertion to verify an…
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0
votes
0 answers

static_assert(sizeof(U64) == sizeof(void*) fails on C++ ARM (32-bit), code causes segmentation fault

I'm compiling RHMiner fon an ARM 32 device (armv7-a) using SSE2NEON, specifically an Android (API 21) device. I am new to C/C++ so this may be a simple question to answer, but I cannot find any resources online matching my situation. The app…
Kris B.
  • 95
  • 1
  • 8
0
votes
1 answer

static_assert'ion that a long and int are the Same Type

So I'm taking a variable from one API, we'll call it long foo and passing it to another API which takes it as the value: int bar. I'm in visual-studio-2017 in which these are effectively the same thing:…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
votes
1 answer

C static_assert first parameter

I wrote this code to test static_assert #include #include #include #include #define static_assert _Static_assert typedef enum {ONE=1, TWO, THREE} num_t; uint8_t Is_Num_Valid(num_t number){ uint8_t i =…
0
votes
1 answer

Checking constant variable's value at the compilation time

For efficient code maintenance I need to make sure that the value at index 0 of an array is a specific predefined value. The following code doesn't work: #define SPECIFIC_ADDR_IDX 0 #define SPECIFIC_ADDR 8 #define NOT_SPECIFIC_ADDR1 12 #define…
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
0
votes
3 answers

_Static_assert replacement to show value in C

Is it possible to have the compiler error/warning diagnostic output a compile-time computed numeric value in C11 or C17 (i.e. not using templates)? The below link does this in C++ using template magic. The intention is to use this as a…
Jetski S-type
  • 1,138
  • 2
  • 16
  • 32
0
votes
1 answer

Why can't I load files from my local machine (file:///) using SRI?

I have some webpages I am trying to load in my browser from file:/// and I have integrity hashes on the JS and CSS. The JS and CSS don't load with the integrity hash present in the and