开发者

C++ Any way to programmatically detect POD-struct?

开发者 https://www.devze.com 2023-03-04 03:00 出处:网络
I have data structure which stores POD-structs (each instantiation stores a single type only, since it is basically an array of a specific POD-struct).Sometimes another dev. will mod开发者_开发问答ify

I have data structure which stores POD-structs (each instantiation stores a single type only, since it is basically an array of a specific POD-struct). Sometimes another dev. will mod开发者_开发问答ify one of these structs, adding or modifying a data type. If a non-POD element is added, e.g. std::string, the data structure blows-up at runtime, because the memory model changes. Is there any way to detect if a class or struct is POD-compliant using compiler defines or a call at run-time (to avoid this maintainence issue) ? I'm using g++ (GCC) 4.2.4.


At runtime probably not, but at compile time, you can use is_pod trait from either C++0x standard library or Boost.TypeTraits.

static_assert(std::is_pod<YourStruct>::value);


You can probably use boost type_traits library and in particular boost::is_pod<T>::value in an static assert.


If you don't have boost or C++0x then you can perhaps use some fact like that C++ does not let to use non-POD as member of union.

0

精彩评论

暂无评论...
验证码 换一张
取 消