以下代码通过:
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怎么可能成为一个呢?
解决方法 让我们看看标准布局的实际规则: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.
[C++14: 9/7]:
A standard-layout class is a class that:
std :: string可能没有公共数据成员(它们会是什么?),这是你用你的T绊倒的地方(因为现在你有私人和公共数据成员;看到大胆的段落).
但据我所知,std :: string没有实际要求是标准布局.这就是您的实施方式.
总结以上是内存溢出为你收集整理的c – 为什么std :: string是标准布局类型?全部内容,希望文章能够帮你解决c – 为什么std :: string是标准布局类型?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)