加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么std :: string是标准布局类型?

发布时间:2020-12-16 10:14:04 所属栏目:百科 来源:网络整理
导读:以这里为例: trivial vs. standard layout vs. POD 以下代码通过: struct T {public: int i;private: int j;};static_assert(! std::is_standard_layoutT::value,""); 但以下情况并非如此: static_assert(! std::is_standard_layoutstd::string::value,""
以这里为例: trivial vs. standard layout vs. POD

以下代码通过:

struct T {
public:
    int i;
private:
    int j;
};

static_assert(! std::is_standard_layout<T>::value,"");

但以下情况并非如此:

static_assert(! std::is_standard_layout<std::string>::value,"");

因此,如果一个类型不是标准布局所需,那么std :: string怎么可能成为一个呢?

解决方法

让我们看看标准布局的实际规则:

[C++14: 9/7]: A standard-layout class is a class that:

  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • has no virtual functions (10.3) and no virtual base classes (10.1),
  • has the same access control (Clause 11) for all non-static data members,
  • has no non-standard-layout base classes,
  • either has no non-static data members in the most derived class and at most one base class with non-static data members,or has no base classes with non-static data members,and
  • has no base classes of the same type as the first non-static data member.

std :: string可能没有公共数据成员(它们会是什么?),这是你用你的T绊倒的地方(因为现在你有私人和公共数据成员;看到大胆的段落).

但据我所知,std :: string没有实际要求是标准布局.这就是您的实施方式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读