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

Ensure the user creates instance of a new subclass

Let's say there is a base class class Base { } and the user has created a new subclass class NewDerivedClass : public Base { } There is also a enum for all the subclasses: enum SubclassId { Derived1_Id, Derived2_Id, ... …
Egor Okhterov
  • 478
  • 2
  • 9
  • 34
0
votes
1 answer

C++ : compile time assert the value of a floating point number

I am using C++ 11. I have a floating point number. float some_float = 3.0; Now I want to compile time check that this number is greater than some value. Say that I want to compile time assert that some_float is greater than 1.0. i am trying…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
0
votes
1 answer

How to give nice static_assert message to users when the template return type prevents it from instantiation?

Let's assume that I want to write function that returns pointer the first element of nonempty container. // REQUIRES: c not empty template auto pointer_to_first(C& c) -> decltype( &(*c.begin()) ){ return nullptr; // TODO:…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
0
votes
2 answers

Triggering void constexpr at compile time?

I have a constexpr function that groups many static_asserts for design contracts. I would like to call it at compile time, without having to create an unused constexpr variable. Here is an example of what I currently have to do…
scx
  • 3,221
  • 1
  • 19
  • 37
0
votes
2 answers

the most clean way to static_assert 3 or more items in oneline

How to static_assert 3 items to be same at compile time like this. union { std::uint32_t multibyte; std::uint8_t bytes[4]; } test; static_assert(sizeof(test) == sizeof(test.multibyte) == sizeof(test.bytes), "Union size mismatch."); So…
ckain
  • 383
  • 5
  • 13
0
votes
1 answer

C++ static_assert to check whether a template had been instantiated with a specific type

Motivation: I'm trying to warn future maintainers that if they do something in the code they must make sure to do something else too Please note that "instantiation" here means template instantiation and not object of class instantiation. class…
cpp114
  • 1
0
votes
2 answers

Compile-time check that all array values are filled

This: constexpr const std::array {{ 0, 1 }}; compiles OK. But how to check (in compile-time) that whole array is filled? May be some static_assert?
vladon
  • 8,158
  • 2
  • 47
  • 91
0
votes
1 answer

Static assets in play java

I have save path in database, when I use it in view @for(photo <- photos){ Cinque Terre } photo.getPath() is not working, I…
Chung Do
  • 79
  • 9
0
votes
1 answer

static_assert always triggering in constexpr

I wrote the following code to find the first index in a tuple containing a given type. #include #include #include #include namespace detail { template
MichaelMitchell
  • 1,069
  • 2
  • 13
  • 38
0
votes
1 answer

How to get failure trace with Junit4

in my Junit test, I use usually "AssertEquals" and when the test fails, the trace is properly displayed in the Failure trace of JUnit/eclipse I would like to know how to get these trace to show it in a file? @Test public void testButtons() { …
laura
  • 155
  • 3
  • 13
0
votes
3 answers

How to initialize a map of arrays of different sizes?

Simple question, how to initialize a map of arrays (or some other container type) of different sizes? For example: enum class code {A,B,C}; enum class res {X1,X2,X3,X4,X5}; std::map name { {code:A, {res::X1,res::X2}}, {code:B,…
CodeBreaker
  • 488
  • 9
  • 18
0
votes
2 answers

c++ define calculations in the text

headerfile.h #define NBIT 128 #define DATASIZE 34 #define STR_HELPER(x) #x #define STR(x) STR_HELPER(x) template class xzcMutatorECB128 { #define POWERLIMINT (NBIT * DATASIZE * 0.7) #define MSG2 "xzcMutatorECB128;…
zizix
  • 369
  • 1
  • 2
  • 5
0
votes
1 answer

static_assert triggering during function name resolution

The following code will static_assert when calling the member function A::Get with a string literal, presumably because the function overloading naming resolution must instantiate the template regardless of whether it is selected. template
0
votes
2 answers

C++ - Static_assert and ability of constexpr functions to evaluate at runtime

I'm reading about constexpr and static_assert features in C++ and one thing seems confusing to me - I've read that constexpr functions are not necessarily always evaluated during compilation and they can sometimes evaluate at runtime. One thing that…
qiubit
  • 4,708
  • 6
  • 23
  • 37
0
votes
2 answers

How to use static_assert used with a member initilizer list

I would want to use static_assert to enforce various limitations on configuration of my class. Earlier I would just use an enum and only allow a single constructor which requires said enum to enforce a limit on my class. This worked fine if I have…
hak8or
  • 488
  • 2
  • 9
  • 28