Is Reference type in C++ a POD type?

后端 未结 3 505
星月不相逢
星月不相逢 2021-02-08 21:33

Is Reference type in C++ a POD type too? Is int& is a POD type? and what about

struct Q { int& i; }

Anyone can help me?

3条回答
  •  野的像风
    2021-02-08 22:32

    No.

    The only way to set a member that references something is via a user-declared constructor, so therefore, your structure is non-POD.

    Update:
    The answer is still no.

    The C++03 standard specifies that "A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and ..." (C++03 standard sect. 9 para 5).

    In C++11, a POD-struct "is a class that is both a trivial class and a standard-layout class, and ..." and a standard-layout class "has no non-static data members of type non-standard-layout class (or array of such types) or reference" (C++11 standard sect. 9 para. 6-9).

    I parse those phrases that end with "or reference" to immediately mean that a POD class cannot contain non-static data members that are of any reference type. The lack of a comma before the "or reference" does give way to other interpretations (e.g., it's a reference to a non-POD class that makes the reference bad, not a reference per se), but I'm sticking with my interpretation.

提交回复
热议问题