【问题标题】:Is std::is_standard_layout_v<T> always true if std::is_trivial_v<T> is true?如果 std::is_trivial_v<T> 为真,则 std::is_standard_layout_v<T> 是否始终为真?
【发布时间】:2020-06-10 08:52:38
【问题描述】:
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_trivial_v&lt;T&gt; 为真,std::is_standard_layout_v&lt;T&gt; 是否始终为真?

  2. 如果没有,有反例吗?

【问题讨论】:

标签: c++ class c++11 typetraits


【解决方案1】:
#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2023-02-16
    • 2016-03-31
    • 2015-07-23
    相关资源
    最近更新 更多