Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::is_standard_layout — checking standard-layout types

std::is_standard_layout checks at compile time whether a type satisfies the standard-layout rules. std::is_standard_layout_v exposes the same result directly as a bool. The trait does not guarantee padding-free storage or a portable binary format.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

// <type_traits> synopsis (provided by the standard library)
namespace std {
    template<class T> struct is_standard_layout;

    template<class T>
    inline constexpr bool is_standard_layout_v = is_standard_layout<T>::value;
}

The <type_traits> header provides the class template std::is_standard_layout<T>. Its value member is a constant of type bool: it is true when T is a standard-layout type. The variable template std::is_standard_layout_v<T> is shorthand for std::is_standard_layout<T>::value, not a type alias. Both forms work in static_assert and if constexpr.

Standard-layout types include scalar types, classes meeting specific rules, arrays of such types, and their cv-qualified versions. Class requirements include having no virtual functions or virtual base classes, the same access control for all non-static data members, and no non-static data members of reference type or of a non-standard-layout class type. This member-type restriction does not apply to static data members. Inheritance has additional restrictions; merely having no virtual functions is insufficient.

The precondition requires std::remove_all_extents_t<T> to be a complete type or possibly cv-qualified void. Do not query a class before its full definition. For void, the result is false. Do not add your own specializations of these traits: doing so causes undefined behavior.

Standard-layout and trivial copyability are separate properties. A true result does not guarantee the absence of padding bytes, an identical ABI, or safe serialization by writing raw bytes. In the example, Point satisfies the rules, whereas Polymorphic fails because of its virtual function; the expected output is true followed by false on separate lines.

Examples

#include <iostream>
#include <type_traits>

struct Point {
    int x;
    int y;
};

struct Polymorphic {
    virtual void draw() {}
};

static_assert(std::is_standard_layout<Point>::value);
static_assert(std::is_standard_layout_v<Point>);
static_assert(!std::is_standard_layout_v<Polymorphic>);

int main() {
    std::cout << std::boolalpha
              << std::is_standard_layout<Point>::value << '\n'
              << std::is_standard_layout_v<Polymorphic> << '\n';
}

Result

true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources