This might not be a solution in the sense you envision it, but it generates an error message which always contains the real size and the expected size close the error message from the static_assert
:
#include <type_traits>
template< typename Type, std::size_t ExpectedSize, std::size_t ActualSize = 0 >
struct validate_size : std::true_type
{
static_assert( ActualSize == ExpectedSize,
"actual size does not match expected size" );
};
template< typename Type, std::size_t ExpectedSize >
struct validate_size< Type, ExpectedSize, 0 >
: validate_size< Type, ExpectedSize, sizeof( Type ) >
{};
int main()
{
static_assert( validate_size< int, 4 >::value, "Oops" );
static_assert( validate_size< int, 5 >::value, "Oops2" );
}
The error message from GCC 4.8:
main.cpp: In instantiation of 'struct validate_size<int, 5ul, 4ul>':
main.cpp:10:8: required from 'struct validate_size<int, 5ul>'
main.cpp:15:43: required from here
main.cpp:6:5: error: static assertion failed: actual size does not match expected size
static_assert( ActualSize == ExpectedSize, "actual size does not match expected size" );
^
The message from Clang also contains the <int, 5ul, 4ul>
-part, check for VS yourself.
Update: And you can obviously keep your interface intact:
#define STATIC_ASSERT_SIZE(T, sz) static_assert(validate_size<T,sz>::value, "")