Coder
Coder

Reputation: 3715

Is it possible to use compile time asserts in C++

I want to use a template for some data processing, but I need the code to be more or less safe when ported.

This might be a problem if sizes of variables grow beyond anticipated values, so I would like to assert at compile time that some assumptions are still valid.

For example, sizeof(long)>sizeof(int), so that if this assumption fails, I could break the build.

I know that standard mandates that long>=int, but this is just an example, don't concentrate on the variable types.

Upvotes: 1

Views: 271

Answers (1)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234434

C++11 has static_assert:

static_assert(sizeof(long) > sizeof(int), "long must be greater than int");

On older compilers you can use Boost.StaticAssert.

BOOST_STATIC_ASSERT(sizeof(long) > sizeof(int));

Upvotes: 2

Related Questions