Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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.

like image 707
lightdee Avatar asked May 10 '11 19:05

lightdee


People also ask

How to define a structure in C++?

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

What is a struct in data structure?

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

What is STRUCT statement in C++?

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.

How to find the address of a structure variable using pointers?

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.


2 Answers

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);
like image 51
Cat Plus Plus Avatar answered Oct 06 '22 18:10

Cat Plus Plus


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

like image 28
David Rodríguez - dribeas Avatar answered Oct 06 '22 18:10

David Rodríguez - dribeas