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

Asserting a Template Argument is an Iterator/Pointer

I have a templatized function that takes in pointers. template void foo(const T* bar){} How can I change foo to ensure that I am being passed an iterator/pointer? I assume there is a static_assert or an enable_if way of doing this, but…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
votes
2 answers

static_assert doesn't work as expected when using std::is_same, std::result_of and std::bind

I'm experiencing a weird behavior when using static_assert for asserting that the return type of a function object is the same as another type. Something like (This code is just for presenting the problem and not actually what i'm trying to do). int…
Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
0
votes
2 answers

Availability of static_assert c++11

I would like to start using static_assert in the codebase that I work on. Unfortunately, not all C++ compilers support them. In the past, we've used a compile-time assert macro that works reasonably for all the compilers I've tried (gleaned from…
MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
0
votes
2 answers

Conditional compilation of templates

I am trying to get static_assert to help me avoid null pointers in C++11. The problem seems to be that C++11 require the compiler to compile templates even if they are not instantiated. I have the following code: #include…
Generic Name
  • 1,083
  • 1
  • 12
  • 19
0
votes
1 answer

How can I determine compile-time that a pointer cast will be offsetted

There are several ways to detect this at run-time but I cannot find a way to determine if a pointer to a class will be offsetted at compile-time. class MyA { public: int m_memberI; }; class MyB { public: double m_memberD; }; class MyC :…
0
votes
2 answers

static_assert negative integer

#include using namespace std; template constexpr int pow2T() { static_assert(fact < 0, "error"); return fact == 0 ? 1 : pow2T() * 2; } constexpr int e2 = pow2T<2>(); int main(int argc, char *argv[]) { cout…
noname7619
  • 3,370
  • 3
  • 21
  • 26
0
votes
1 answer

Why does the `static_assert` always get invoked?

If USE_STATIC_ASSERT is 0, this works as expected (getting indexed type from the list). If 1 the static_assert() is always tripped. I would have thought that the static_assert() would only happen if all the typenames were exhausted. Why is this…
Adrian
  • 10,246
  • 4
  • 44
  • 110
0
votes
1 answer

How do I test that static assert does indeed assert for "false"?

Suppose I have a custom static assert implementation (because I need to target a compiler that doesn't have static_assert built in). I want to craft a test that checks that MY_STATIC_ASSERT(false); indeed asserts. If I just write such code - it…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
0
votes
0 answers

assure correct order of initialization of const members in an initializer list via static_assert

I have a c++ class with const& members which are initialized in the initialization list of the constructor. The order of the initialization of the members is crucial, for which I rely on the order of member declarations in the class declaration and…
frithjofh
  • 11
  • 2
0
votes
1 answer

Static assert in template parameters

I would like to ask if it is possible to insert static assert into template parameters. Let´s say I want to create class StaticArray and I want to make it impossible for users to instantiate this class with size equal to 0. Is there any way to…
Alexander Bily
  • 925
  • 5
  • 18
0
votes
1 answer

static_assert that checks if template parameter type is accepted by another function

I have a function template defined as follows: template Test &operator<<(const T &data) { std::cout << data << std::endl; return *this; } As you can see, I print out to the console data using std::cout, which type is…
GuiTeK
  • 1,561
  • 5
  • 20
  • 39
0
votes
1 answer

How do I access a constexpr created type as another type while staying constexpr? (using with static_assert)

I've been trying to figure this one out, and thought it would be a fun one to take a look at :) Ok, so I'm creating a type as constexpr using bitfields. Since bitfields can change from one implementation or platform to another, I want to…
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
0
votes
3 answers

Is there any way to enforce in C++ (at compile-time) that a derived class defines a nested type?

For example, I have a base class Event and I want to ensure that every class derived from Event has defined an enum class Type member, so that T::Type is a valid type for any class T derived from Event.
Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
0
votes
2 answers

static_assert even if member is not instantiated

When putting a with a hardcoded value, it is triggered even if the function it's in isn't instantiated. Is this behavior correct or do I misunderstand how static assert works? #include template struct Helper { static void…
nishantjr
  • 1,788
  • 1
  • 15
  • 39
0
votes
3 answers

static_assert unexpected behavior

I have an overloaded operator& for my class where I do a static assert if the parameter is a pointer. class test { public: template friend inline test& operator&(test& so, T const& t) { std::cout << "is_pointer : " <<…
user1810087
  • 5,146
  • 1
  • 41
  • 76
1 2 3
30
31