Is a link type in C ++ a POD type?

Is a reference type in C ++ a POD type? Is int& POD type? and what about

 struct Q { int& i; } 

Can anybody help me?

+7
c ++ pod
source share
3 answers

Not.

The only way to set an element that refers to something is through a constructor declared by the user, so your structure is not a POD.

Update:
The answer is still no.

The C ++ 03 standard states that "POD-struct is an aggregate class that does not have non-static data members such as non-POD-struct, non-POD-union (or an array of such types) or references, and ..." (C ++ 03 standard, section 9, paragraph 5).

In C ++ 11, a POD-struct "is a class that is both a trivial class and a standard layout class and ...", and the standard layout class "does not contain non-static data (or an array of such types) or a link" ( C ++ 11 standard, clause 9, clause 6-9).

I am parsing those phrases that end with the words “or link” to immediately mean that the POD class cannot contain non-static data elements that have any reference type. The absence of a comma before “or link” gives way to other interpretations (for example, this is a reference to a class other than POD, which makes the link bad, and not the link itself), but I stick to my interpretation.

+9
source share

There is a standard way (using C ++ 11) to determine what is at compile time.

 #include <iostream> #include <type_traits> struct Q { int& i; }; int main() { std::cout << std::is_pod<int>::value << "\t"; std::cout << std::is_pod<int&>::value << "\t"; std::cout << std::is_pod<Q>::value << "\n"; } 

Demo: http://ideone.com/PECzfT

The output will be 1 0 0, so there is no reference to int not POD.

+6
source share

A POD type is any internal type or a collection of internal types (basically, everything that does not have a custom constructor, destructor, copy constructor, copy assignment operator, and non-static element pointer types (more details here )

Since an element that refers to something must be set using a custom constructor, this will violate the first requirement. In short, a structure declaring a reference type as a member is not a POD type.

+4
source share

All Articles