Executing the following code:
#include <iostream>
#include <type_traits>
struct s_ref {
int &foo;
};
struct s_ptr {
int *foo;
};
int main(int argc, char *argv[])
{
std::cout << "s_ref is_standard_layout:" << std::is_standard_layout<struct s_ref>::value << std::endl;
std::cout << "s_ptr is_standard_layout:" << std::is_standard_layout<struct s_ptr>::value << std::endl;
return 0;
}
results in:
s_ref is_standard_layout:0
s_ptr is_standard_layout:1
Based on the uses of standard layout (i.e.: "Standard layout types are useful for communicating with code written in other programming languages") this makes sense, but I'm not sure which is the rule that is violated:
A standard-layout class is a class (defined with class, struct or union) that:
has no virtual functions and no virtual base classes.
has the same access control (private, protected, public) for all its non-static data members.
either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members.
its base class (if any) is itself also a standard-layout class.
And, has no base classes of the same type as its first non-static data member.
Edit: the quote is from: http://www.cplusplus.com/reference/type_traits/is_standard_layout/, but http://en.cppreference.com/w/cpp/concept/StandardLayoutType is also similar.
The point of the C++ standard's concept of a standard layout class is that an instance of such a class can be reliably accessed as or copied to bytes, which, as the C++11 standard notes in its §9/9, makes such a class
” useful for communicating with code written in other programming languages
However, the C++ standard does not require a reference to use storage at all. It's not an object. You can't take its address. So it can't be (reliably) copied to bytes, or accessed as bytes. And so it's not compatible with the notion of standard layout classes.
In the formal,
C++11 §9/7:” A standard-layout class is a class that:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
I don't know where you got that quote from, but it misses out the relevant rule from the standard.
(N3337) [class]/7:
A standard-layout class is a class that:— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
— has no virtual functions (10.3) and no virtual base classes (10.1),
— has the same access control (Clause 11) for all non-static data members,
— has no non-standard-layout base classes,
— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
— has no base classes of the same type as the f irst non-static data member.108
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