c++ 11-从aggrrgate的汇总列表限制

发布于 2025-02-12 03:23:12 字数 1451 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2025-02-19 03:23:12

相同或派生的类型

,这意味着将使用默认复制构建器。这就是为什么这两个规则之间没有矛盾的原因

of the same or derived type

That means that default copy-constructor will be used. That's why there is no contradiction between these two rules

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文