<legend id='Pz38O'><style id='Pz38O'><dir id='Pz38O'><q id='Pz38O'></q></dir></style></legend>

      <bdo id='Pz38O'></bdo><ul id='Pz38O'></ul>
      <tfoot id='Pz38O'></tfoot>

      <small id='Pz38O'></small><noframes id='Pz38O'>

    1. <i id='Pz38O'><tr id='Pz38O'><dt id='Pz38O'><q id='Pz38O'><span id='Pz38O'><b id='Pz38O'><form id='Pz38O'><ins id='Pz38O'></ins><ul id='Pz38O'></ul><sub id='Pz38O'></sub></form><legend id='Pz38O'></legend><bdo id='Pz38O'><pre id='Pz38O'><center id='Pz38O'></center></pre></bdo></b><th id='Pz38O'></th></span></q></dt></tr></i><div id='Pz38O'><tfoot id='Pz38O'></tfoot><dl id='Pz38O'><fieldset id='Pz38O'></fieldset></dl></div>

      为什么我不能大括号初始化从另一个结构派生的结构?

      Why can I not brace initialize a struct derived from another struct?(为什么我不能大括号初始化从另一个结构派生的结构?)
      <i id='Ssocn'><tr id='Ssocn'><dt id='Ssocn'><q id='Ssocn'><span id='Ssocn'><b id='Ssocn'><form id='Ssocn'><ins id='Ssocn'></ins><ul id='Ssocn'></ul><sub id='Ssocn'></sub></form><legend id='Ssocn'></legend><bdo id='Ssocn'><pre id='Ssocn'><center id='Ssocn'></center></pre></bdo></b><th id='Ssocn'></th></span></q></dt></tr></i><div id='Ssocn'><tfoot id='Ssocn'></tfoot><dl id='Ssocn'><fieldset id='Ssocn'></fieldset></dl></div>
          <tbody id='Ssocn'></tbody>

        <small id='Ssocn'></small><noframes id='Ssocn'>

            <bdo id='Ssocn'></bdo><ul id='Ssocn'></ul>

              <legend id='Ssocn'><style id='Ssocn'><dir id='Ssocn'><q id='Ssocn'></q></dir></style></legend>
              1. <tfoot id='Ssocn'></tfoot>
                本文介绍了为什么我不能大括号初始化从另一个结构派生的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当我运行此代码时:

                struct X {
                    int a;
                };
                
                struct Y : public X {};
                
                X x = {0};
                Y Y = {0};
                

                我明白了:

                error: could not convert ‘{0}’ from ‘<brace-enclosed initializer list>’ to ‘Y’
                

                为什么花括号初始化对基类有效,而对派生类无效?

                Why does brace initialization work for the base class but not the derived class?

                推荐答案

                针对C++17之前的C++标准版本的回答:

                您的问题与聚合初始化有关:structX 是一个聚合,而 struct Y 不是.以下是关于聚合 (8.5.1) 的标准引用:

                Your problem has to do with aggregate initialization: struct X is an aggregate while struct Y is not. Here is the standard quote about aggregates (8.5.1):

                聚合是一个数组或类(第 9 条),没有用户提供的构造函数(12.1),没有用于非静态数据成员的大括号或等号初始化器(9.2),没有私有或受保护的非静态数据成员(第 11 条),没有基类(第 10 条),也没有虚函数(10.3).

                An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

                这个子句指定如果一个 class 有一个基类,那么它不是一个聚合.这里,struct Ystruct X 作为基类,因此不能是聚合类型.

                This clause specifies that if a class has a base class, then it's not an aggregate. Here, struct Y has struct X as a base class and thus cannot be an aggregate type.

                关于您遇到的特定问题,请从标准中提取以下条款:

                Concerning the particular problem you have, take the following clause from the standard:

                当一个聚合被初始化列表初始化时,如 8.5.4 中所指定的,初始化列表的元素被作为聚合成员的初始化,按照递增的下标或成员顺序.每个成员都是从相应的初始化子句复制初始化的.如果 initializer-clause 是一个表达式,并且需要进行收缩转换(8.5.4)来转换该表达式,则程序格式错误.

                When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order. Each member is copy-initialized from the corresponding initializer-clause. If the initializer-clause is an expression and a narrowing conversion (8.5.4) is required to convert the expression, the program is ill-formed.

                当您执行X x = {0} 时,使用聚合初始化将a 初始化为0.但是,当您执行 Y y = {0} 时,由于 struct Y 不是聚合类型,编译器将寻找合适的构造函数.由于隐式生成的构造函数(默认、复制和移动)都不能对单个整数执行任何操作,因此编译器会拒绝您的代码.

                When you do X x = {0}, aggregate initialization is used to initialize a to 0. However, when you do Y y = {0}, since struct Y is not an aggregate type, the compiler will look for an appropriate constructor. Since none of the implicitely generated constructors (default, copy and move) can do anything with a single integer, the compiler rejects your code.

                关于这个构造函数查找,来自 clang++ 的错误消息对于编译器实际尝试执行的操作更加明确(在线示例):

                Concerning this constructors lookup, the error messages from clang++ are a little bit more explicit about what the compiler is actually trying to do (online example):

                Y Y = {0};
                  ^   ~~~
                
                main.cpp:5:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const Y &' for 1st argument
                
                struct Y : public X {};
                       ^
                
                main.cpp:5:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'Y &&' for 1st argument
                
                struct Y : public X {};
                       ^
                
                main.cpp:5:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
                


                注意有一个提案 扩展聚合初始化以支持您的用例, 将其纳入 C++17.如果我读对了,它会使您的示例在您期望的语义下有效.所以......你只需要等待一个符合 C++17 的编译器.


                Note that there is a proposal to extend aggregate initialization to support your use case, and it made it into C++17. If I read it correctly, it makes your example valid with the semantics you expect. So... you only have to wait for a C++17-compliant compiler.

                这篇关于为什么我不能大括号初始化从另一个结构派生的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                <i id='8gRFo'><tr id='8gRFo'><dt id='8gRFo'><q id='8gRFo'><span id='8gRFo'><b id='8gRFo'><form id='8gRFo'><ins id='8gRFo'></ins><ul id='8gRFo'></ul><sub id='8gRFo'></sub></form><legend id='8gRFo'></legend><bdo id='8gRFo'><pre id='8gRFo'><center id='8gRFo'></center></pre></bdo></b><th id='8gRFo'></th></span></q></dt></tr></i><div id='8gRFo'><tfoot id='8gRFo'></tfoot><dl id='8gRFo'><fieldset id='8gRFo'></fieldset></dl></div>
                • <bdo id='8gRFo'></bdo><ul id='8gRFo'></ul>

                    <tbody id='8gRFo'></tbody>
                  <tfoot id='8gRFo'></tfoot>

                    <small id='8gRFo'></small><noframes id='8gRFo'>

                  • <legend id='8gRFo'><style id='8gRFo'><dir id='8gRFo'><q id='8gRFo'></q></dir></style></legend>