xmllmx
xmllmx

Reputation: 42379

Is std::is_standard_layout_v<T> always true if std::is_trivial_v<T> is true?

template<typename T>
void f()
{
    if constexpr (std::is_trivial_v<T>)
    {
        // Does the following line never fail?
        static_assert(std::is_standard_layout_v<T>); 
    }
}
  1. Is std::is_standard_layout_v<T> always true if std::is_trivial_v<T> is true?

  2. If no, is there any counterexamples?

Upvotes: 1

Views: 488

Answers (1)

user7860670
user7860670

Reputation: 37600

#include <type_traits>

class type
{
    public: int x;
    private: int y;
};

static_assert(std::is_trivial_v< type >);
// not standard layout because different access level of x and y
static_assert(not std::is_standard_layout_v< type >);

https://godbolt.org/z/xz3xLy

Upvotes: 2

Related Questions