C++11stlintermediate
std::is_standard_layout (C++11 Addition)
C++11 · Modern Revolution · Compiler flag: -std=c++11
Explanation
Compile-time check that determines whether a type has a standard memory layout compatible with C structs.
Real-World Scenario
Ensure a struct can be safely passed to a C library via FFI.
Quick Snippet
snippet.cpp
#include <type_traits>
struct CCompatible { int x; float y; };
static_assert(std::is_standard_layout<CCompatible>::value, "C-compatible layout");Full Example
is_standard_layout.cpp
// CodeNebula - C++11 Example: is_standard_layout
// Compile: g++ -std=c++11 -Wall -Wextra -o example is_standard_layout.cpp
// Demonstrates std::is_standard_layout which checks if a type has a
// memory layout compatible with C. Standard layout types can be used
// for C interop and have predictable member offsets.
#include <iostream>
#include <type_traits>
struct StandardLayout {
int x;
double y;
char z;
};
struct NonStandard {
virtual void foo() {} // virtual function breaks standard layout
int x;
};
struct MixedAccess {
public:
int x;
private:
int y; // mixed access specifiers break standard layout
};
struct DerivedSimple : StandardLayout {
// no additional non-static data members -> still standard layout
};
struct DerivedWithData : StandardLayout {
int extra; // data in both base and derived -> non-standard layout
};
int main() {
std::cout << "=== std::is_standard_layout ===" << std::endl;
std::cout << "int: " << std::is_standard_layout<int>::value << std::endl;
std::cout << "StandardLayout: " << std::is_standard_layout<StandardLayout>::value << std::endl;
std::cout << "NonStandard: " << std::is_standard_layout<NonStandard>::value << std::endl;
std::cout << "MixedAccess: " << std::is_standard_layout<MixedAccess>::value << std::endl;
std::cout << "DerivedSimple: " << std::is_standard_layout<DerivedSimple>::value << std::endl;
std::cout << "DerivedWithData: " << std::is_standard_layout<DerivedWithData>::value << std::endl;
static_assert(std::is_standard_layout<int>::value, "int is standard layout");
static_assert(std::is_standard_layout<StandardLayout>::value, "simple struct is SL");
static_assert(!std::is_standard_layout<NonStandard>::value, "virtual breaks SL");
std::cout << "\nAll checks passed!" << std::endl;
return 0;
}
// Expected output:
// === std::is_standard_layout ===
// int: 1
// StandardLayout: 1
// NonStandard: 0
// MixedAccess: 0
// DerivedSimple: 1
// DerivedWithData: 0
//
// All checks passed!
Run command
g++ -std=c++11 -Wall -Wextra -o example is_standard_layout.cpp