lightdee
lightdee

Reputation: 507

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

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 modify 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.

Upvotes: 18

Views: 2846

Answers (3)

Öö Tiib
Öö Tiib

Reputation: 11014

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.

Upvotes: 5

Cat Plus Plus
Cat Plus Plus

Reputation: 129894

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);

Upvotes: 25

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

Upvotes: 9

Related Questions