Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static assert for const variables?

Static asserts are very convenient for checking things in compile time. A simple static assert idiom looks like this:

template<bool> struct StaticAssert;
template<> struct StaticAssert<true> {};

#define STATIC_ASSERT(condition) do { StaticAssert<(condition)>(); } while(0)

This is good for stuff like

STATIC_ASSERT(sizeof(float) == 4)

and:

#define THIS_LIMIT (1000)
...
STATIC_ASSERT(THIS_LIMIT > OTHER_LIMIT);

But using #define is not the "C++" way of defining constants. C++ would have you use an anonymous namespace:

namespace {
    const int THIS_LIMIT = 1000;
}

or even:

static const int THIS_LIMIT = 1000;

The trouble with this is that with a const int you can't use STATIC_ASSERT() and you must resort to a run-time check which is silly.

Is there a way to properly solve this in current C++?
I think I've read C++0x has some facility to do this...


EDIT

Ok so this

static const int THIS_LIMIT = 1000;
...
STATIC_ASSERT(THIS_LIMIT > 0);

compiles fine
But this:

static const float THIS_LIMIT = 1000.0f;
...
STATIC_ASSERT(THIS_LIMIT > 0.0f);

does not.
(in Visual Studio 2008)

How come?

like image 426
shoosh Avatar asked May 25 '10 07:05

shoosh


People also ask

Can static variables be const?

So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.

How do you use static assert?

The C++ 11 standard introduced a feature named static_assert() which can be used to test a software assertion at the compile time. Syntax: static_assert( constant_expression, string_literal ); Parameters: constant_expression: An integral constant expression that can be converted to a Boolean.

What is the difference between assert () and static_assert ()? Select one?

Answer: Static_assert is evaluated at compile time as against the assert () statement that is evaluated at run time. Static_assert has been incorporated in C++ from C++11 onwards. It takes the conditional expression and a message to be displayed as arguments.

Where is static_assert?

static_assert is a keyword defined in the <assert. h> header. It is available in the C11 version of C. static_assert is used to ensure that a condition is true when the code is compiled.


2 Answers

Why, you can still static assert with const int:

#define static_assert(e) extern char (*ct_assert(void)) [sizeof(char[1 - 2*!(e)])]
static_assert( THIS_LIMIT > OTHER_LIMIT )

Also, use boost!

BOOST_STATIC_ASSERT( THIS_LIMIT > OTHER_LIMIT )

... you'll get a lot nicer error messages...

like image 69
Kornel Kisielewicz Avatar answered Sep 19 '22 13:09

Kornel Kisielewicz


static_assert is a compiler feature in C++0x so as long as you've got a relatively up-to-date compiler you can use that. Watch out for doing #define static_assert(x) ..., because it's a real keyword in C++0x so you'd be permanently hiding the compiler feature. Also, C++0x static_assert takes two parameters (eg. static_assert(sizeof(int) == 4, "Expecting int to be 4 bytes")), so you could cause yourself problems trying to switch in future if you use that #define.

like image 26
AshleysBrain Avatar answered Sep 21 '22 13:09

AshleysBrain