От: | cppnick | ||
Дата: | 08.12.11 17:19 | ||
Оценка: |
template<typename T, size_t sizeofT, size_t expected>
struct __sizeof_assert
{
char _[sizeofT == expected];
};
#define sizeof_assert(T, expected_size) static const __sizeof_assert<T, sizeof(T), expected_size> __SIZEOF_ASSERT_ ## __LINE__; static_assert(sizeof(T) == expected_size, "wrong type size")
// использование
sizeof_assert(uint32_t, 4);
1>hxx(1) : warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1> cpp(4) : see reference to class template instantiation '__sizeof_assert<T,sizeofT,expected>' being compiled
1> with
1> [
1> T=std::uint32_t,
1> sizeofT=4,
1> expected=3
1> ]
1>cpp(4) : error C2118: negative subscript
t.cpp: In instantiation of '__sizeof_assert<int, 4u, 1u>':
t.cpp:10: instantiated from here
Line 4: error: ISO C++ forbids zero-size array