I need to make sure a template struct is exactly the size of its members. static_assert
seems to be the tool of choice here. However, I cannot use a static_assert
inside the struct itself, because the size is not yet known there. Here is what I would like to have:
template<typename T1,typename T2>
struct foo {
T1 v1;
T2 v2;
// Doesn't compile, invalid application of sizeof to incomplete type
static_assert(sizeof(foo<T1,T2>)==sizeof(T1)+sizeof(T2),"Struct size invalid");
};
This doesn't work. So how to do it? I don't want to burden people that instantiate the template to check themselves in each instantiation. The check should be fully automatic whenever the struct is instantiated.