如果 std::is_standard_layout_v<T> 为真,那么 std::is_trivial_v<T> 是否总是为真?

问题描述 投票:0回答:1
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. 是否 std::is_standard_layout_v<T> 始终为真,如果 std::is_trivial_v<T> 是真的吗?

  2. 如果不是,有没有反例?

c++ class c++11 typetraits c++-concepts
1个回答
3
投票
#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.orgzxz3xLy。

© www.soinside.com 2019 - 2024. All rights reserved.