Compile-time statements in C ++?

Recently, I came to the conclusion that you need to have statements at the compilation stage in C ++ to make sure that the sizes of the two types are equal.

I found the following macro on the Internet (claimed to come from the Linux kernel):

#define X_ASSERT(condition) ((void)sizeof(char[1 - 2*!!(condition)]))

which I used like this:

X_ASSERT(sizeof(Botan::byte) != sizeof(char));

I get interested - although this works, is there a cleaner way to do this? (obviously, there is more than one way, as it is) Are there advantages or disadvantages of certain methods?

+5
source share
5 answers

, Boost StaticAssert. ( , ), , , , , . , , .

+6

++ 0x static_assert, . ,

static_assert(sizeof(Botan::byte) != 1, "byte type has wrong size");

Visual ++ 2010 static_assert, g++ 4.3 ( ) Intel ++ 11.0.

+10

#error (. ), , a #if " ", .

0

All Articles