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 std classes not use static_assert on non-copyable types?

Why does the std library not use these instead? Currently if a call is made to the copy constructor on a non-copyable object, the error message can be a little 'cryptic' or confusing to someone who has never encountered one before. The first time I…
Igneous01
  • 719
  • 1
  • 10
  • 24
0
votes
2 answers

constexpr template functions don't see member array sizes as const expressions

Both clang and gcc fail to compile the code below when ArrayCount is a template. This seems wrong, especially in light of the fact that the sizeof ArrayCount solution work. The template version of ArrayCount is normally a better solution, but it's…
ThreeBit
  • 608
  • 6
  • 17
0
votes
6 answers

C Compile-Time assert with constant array

I have a very big constant array that is initialized at compile time. typedef enum { VALUE_A, VALUE_B,...,VALUE_GGF } VALUES; const int arr[VALUE_GGF+1] = { VALUE_A, VALUE_B, ... ,VALUE_GGF}; I want to verify that the array is initialized…
Eldad
  • 1,067
  • 16
  • 36
0
votes
1 answer

C++ short enum problems with InterlockedCompareExchange16 (with VS2012)

Having referenced this question: Can an enum class be converted to the underlying type?. In my code I have effectively: enum class STATE : short { EMPTY, PRESENT, PARTIAL, }; volatile STATE state; Then I write a typedef and a…
Coder_Dan
  • 1,815
  • 3
  • 23
  • 31
0
votes
2 answers

C++11 is there a way to test method access level statically?

C++11 adds lots of new class templates which allow to test type traits statically, i.e. detect problems at compile time. I'm writing a test for a class and I need to make sure a given method is public. The "dynamic" solution is to create an object…
0
votes
5 answers

How to do static_assert with macros?

I have tried to use this suggestion to do a static assert, but I do not get a compilation error if I use it within a method of a template. The example follows : #include #define STATIC_ASSERT(expr, msg) \ { …
BЈовић
  • 62,405
  • 41
  • 173
  • 273
0
votes
1 answer

C++ Static type checking (BOOST) incorrectly flagged by Eclipse CDT

So, I want to use the BOOST libraries to static-time check that my templates are being used by certain base classes, for example: template class A { // Code here } So, I wanted to use BOOST_STATIC_ASSERT( (…
Doug
  • 775
  • 2
  • 11
  • 24
-1
votes
1 answer

Is there a way to static_assert a variable reference given in a template parameter?

struct Config { int version = 1; }; template struct Peripheral { const Config config_ = config; static_assert(config_.version > 1, "Config version must be greater than 1"); /* ... */ }; Config…
-1
votes
1 answer

Refactoring Code to assert at compile time instead of throwing an exception at runtime

Consider the following code example, a simple template class wrapper with basic overloaded arithmetic operators. In this class's operator/ I'm using the ternary operator to throw an exception if division by 0 is detected otherwise, I'm returning the…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
-1
votes
1 answer

Check if an expression compiles including all implicit conversion

Consider the following code: void f(auto& i, auto& j) { static_assert(/* SOMETHING */, ""); // function body here... } I want the /* SOMETHING */ part to check whether the following code compiles (taking into account all the standard rules,…
Vincent
  • 57,703
  • 61
  • 205
  • 388
-1
votes
1 answer

Avoid multiple function member call on an instance in C++

I wrote a logger class for my program but I want to force the client to use my class in a specific way. Here is my Logger class: #ifndef __LOGGER_HPP #define __LOGGER_HPP #include #include enum LoggerLevel { ERROR, …
didil
  • 693
  • 8
  • 22
-2
votes
2 answers

static_assert usage in C++ vs C

This is a part of a huge project, so I post an excerpt from a cc file (only one static_assert is needed in the real code, I just experimented with it): namespace large { static_assert(sizeof(void *) == 4, "64-bit code generation is not…
TT_ stands with Russia
  • 1,785
  • 3
  • 21
  • 33
-2
votes
1 answer

Compilation warning for Objects that contain non Copyable objects are unreadable

I've asked a similiar question before but now I'd like to be more specific. The problem I face is that I have an object that contains a non copyable object and when someone wants to use my interface and he does not use it well (does try to use the…
Alon
  • 1,776
  • 13
  • 31
-3
votes
2 answers

How can I print the type of an object as part of a static_assert statement?

I have written some type conversion operators which only make sense in the context of a subset of types. An example is below explicit virtual operator DataId() const { static_assert(std::is_same_v>, "std::is_same_v
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
-8
votes
1 answer

decltype in static_assert

Why this (static_assert) in a definition of a class doesn't work? template struct X { static_assert(std::is_same::value,"Different types not allowed"); }; int…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
1 2 3
30
31