c++ 11-从aggrrgate的汇总列表限制
on cppreference.com的此页面我阅读了以下内容:
如果t是汇总类,而支撑式列表有一个 相同或派生类型的元素(可能是CV合格), 对象是从该元素初始化的(通过 复制列表初始化,或通过直接定义 直接列表初始化)。
但是此页面
聚合是以下类型之一:
- 数组类型
- 类型(通常是结构或联合),具有
没有私人数据成员
没有用户提供,继承或显式构造函数
但是如果汇总类没有用户定义的构造函数,则如何以上面规定的方式初始化它?我的意思是,并不是说聚合成员正在从初始化器的成员那里获得其价值,而是从初始化器中明确地提到了构造函数。
PS1 似乎这是一个很好的例证:
#include <iostream>
#include <type_traits>
class A {
public:
int a;
int b;
//this one does nothing to the code below:
//A() = delete;
//this one does BOOM
//A(A &other) = delete;
private:
};
int
main()
{
if( !std::is_aggregate<A>::value ) {
std::cout << "A is not aggregate!" << std::endl;
}
A a = { 1, 2 };
A b = { a };
std::cout << "a is " << a.a << " " << a.b << std::endl;
std::cout << "b is " << b.a << " " << b.b << std::endl;
}
因此,我想,复制构造函数。
ps2
我在没有std :: is_aggregate
和c ++ 11标志的情况下具有相同的行为。
On this page of the cppreference.com I read the following:
If T is an aggregate class and the braced-init-list has a single
element of the same or derived type (possibly cv-qualified), the
object is initialized from that element (by copy-initialization for
copy-list-initialization, or by direct-initialization for
direct-list-initialization).
But this page states this:
An aggregate is one of the following types:
- array type
- class type (typically, struct or union), that has
no private data members
no user-provided, inherited, or explicit constructors
But if an aggregate class has no user defined constructors, how it could be initialized in a manner stipulated above? I mean, it is not saying that aggregate members are getting their values from the members of the initializer, it explicitly mentions the constructor from the initializer.
PS1
It seems like this one is a good exemplification:
#include <iostream>
#include <type_traits>
class A {
public:
int a;
int b;
//this one does nothing to the code below:
//A() = delete;
//this one does BOOM
//A(A &other) = delete;
private:
};
int
main()
{
if( !std::is_aggregate<A>::value ) {
std::cout << "A is not aggregate!" << std::endl;
}
A a = { 1, 2 };
A b = { a };
std::cout << "a is " << a.a << " " << a.b << std::endl;
std::cout << "b is " << b.a << " " << b.b << std::endl;
}
So, copy constructor it is, I guess.
PS2
I have the same behavior when compiling without std::is_aggregate
and with c++11 flag on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
,这意味着将使用默认复制构建器。这就是为什么这两个规则之间没有矛盾的原因
That means that default copy-constructor will be used. That's why there is no contradiction between these two rules