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.
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows − struct [structure tag] { member definition; member definition;... member definition; } [one or more structure variables];
Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title; Author; Subject; Book ID; Defining a Structure. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than ...
The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows − The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition.
To find the address of a structure variable, place the '&'; operator before the structure's name as follows − To access the members of a structure using a pointer to that structure, you must use the → operator as follows − Let us re-write the above example using structure pointer.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With