std::is_standard_layout

From Cppreference

Jump to: navigation, search
Defined in header <type_traits>

template< class T >
struct is_standard_layout;
(C++11 feature)

If T is a standard layout type (that is, a scalar type, a standard-layout class, or an array of such type/class, possibly cv-qualified), provides the member constant value equal true. For any other type, value is false.

A standard-layout class is a class that

1. has no non-static data members that aren't standard-layout

2. has no virtual functions and no virtual base classes

3. has the same access control for all non-static data members

4. has no base classes that aren't standard-layout

5. either has no base class with non-static data members or has no non-static data members in the most derived class and only one base with them

6. has no base classes of the same type as the first non-static data member

Contents

Inherited from std::integral_constant

Member constants

value true if T is a standard-layout type , false otherwise
(public static member constant)

Member functions

operator bool converts the object to bool, returns value
(public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>

[edit] Notes

A pointer to a standard-layout class may be converted (with reinterpret_cast) to a pointer to its first non-static data member and vice versa.

If a standard-layout union holds two or more standard-layout structs, it is permitted to inspect the common initial part of them.

The macro offsetof can only be used with standard-layout classes.

[edit] Example

#include <iostream>
#include <type_traits>
 
struct A {
    int m;
};
 
struct B {
    int m1;
private:
    int m2;
};
 
struct C {
    virtual void foo();
};
 
int main()
{
    std::cout << std::boolalpha;
    std::cout << std::is_standard_layout<A>::value << '\n';
    std::cout << std::is_standard_layout<B>::value << '\n';
    std::cout << std::is_standard_layout<C>::value << '\n';
}

Output:

true
false
false

[edit] See also

is_trivially_copyable (C++11)
checks if a type is trivially copyable
(class template)
is_pod (C++11)
checks if a type is plain-old data (POD) type
(class template)
offsetof
byte offset from the beginning of a standard-layout type to specified member
(function macro)