I've been using static_assert
(and the variants before standardization) quite heavily. One use that I'm sure many of us have them put to is ensuring that the size of sensitive data structures remain as assumed across platforms and configurations. For example:
class SizeSensitiveClass
{
// ...
};
static_assert (sizeof(SizeSensitiveClass) == 18, "Check the size!");
Now, I've written a convenience macro to help with this particular use:
#define STATIC_ASSERT_SIZE(T, sz) (sizeof(T) == (sz), "Size of '" #T "' doesn't match the expected value.")
Used like this:
STATIC_ASSERT_SIZE (SizeSensitiveClass, 18);
Which produces this output: (at compile time, obviously, in form of compile error)
Size of 'SizeSensitiveClass' doesn't match the expected value.
This is OK and nice, but I was wondering whether I can extend the implementation of this macro (keeping the interface intact) to output the current size and the expected size of the data structure as well. Ideally, the output should look something like:
Size of 'SizeSensitiveClass' doesn't match the expected value (20 vs. 18).
Even the current size would be extremely convenient. Is this possible?
I'm using VC12 (Visual C++ 2013) and GCC 4.8.1. I'd appreciate any solutions/techniques/methods that would be portable to at least these two.
I should mention that I have tried the common "stringize" trick, but it doesn't work (as one would have expected it not to.) It just produces the literal string sizeof(T)
in the output.
I have a vague notion that this might be implemented using constexpr
s (to generate the message string) but I'm not familiar with them.