简体   繁体   English

数组初始值设定项必须是未显示的初始化列表错误

[英]Array initializer must be an initializer list error not shown

I am having a hard time trying to figure out why this code seems to work. 我很难弄清楚为什么这段代码似乎有效。 Shouldn't I get an "array initializer must be an initializer list" error? 我不应该得到“数组初始化器必须是初始化列表”错误?

#include <iostream>

class B {
public:
  B() { std::cout << "B Constructor " << std::endl ; }
  B(const B&) { std::cout << "B Copy Constructor " << std::endl ; }
  ~B() { std::cout << "B Destructor " << std::endl ; }
} ;

class A {
public:
  A() { std::cout << "A Constructor " << std::endl ; }
  A(const A& other) : bs(other.bs)
        { std::cout << "A Copy Constructor " << std::endl ;}
  ~A() { std::cout << "A Destructor " << std::endl ; }
private:
  B bs[12] ;
};


int main() {
    A a ;
    A b(a) ;
    return 0 ;
}

The output of g++ --version is g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18) g++ --version的输出是g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)

It is ill-formed, and GCC has a bug. 它是不正确的,GCC有一个bug。 You specify GCC 4.4.7, so I'm going to base this answer on C++11. 你指定GCC 4.4.7,所以我将把这个答案建立在C ++ 11上。 The meaning of an initializer is determined by [dcl.init]/16 : 初始化程序的含义由[dcl.init] / 16确定:

The semantics of initializers are as follows. 初始化器的语义如下。 The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. 目标类型是要初始化的对象或引用的类型,源类型是初始化表达式的类型。 If the initializer is not a single (possibly parenthesized) expression, the source type is not defined. 如果初始化程序不是单个(可能带括号的)表达式,则不定义源类型。

  • the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized ([dcl.init.list]). 初始化程序是(非括号的)braced-init-list,对象或引用是列表初始化的([dcl.init.list])。
  • If the destination type is a reference type, see [dcl.init.ref]. 如果目标类型是引用类型,请参见[dcl.init.ref]。
  • If the destination type is an array of characters, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string literal, see [dcl.init.string]. 如果目标类型是字符数组,char16_t数组,char32_t数组或wchar_t数组,并且初始值设定项是字符串文字,请参见[dcl.init.string]。
  • If the initializer is () , the object is value-initialized. 如果初始化程序为() ,则对象进行值初始化。
  • Otherwise, if the destination type is an array, the program is ill-formed. 否则,如果目标类型是数组,则程序格式错误。
  • [... Continued but irrelevant here ... ] [......继续但不相关......]

The bullets must be checked in order to determine the meaning of an initializer for a target type. 必须检查项目符号,以确定目标类型的初始值设定项的含义。 Since you use (other.bs) as an initializer, and the target type is an array, the only applicable bullet is the one I highlighted. 由于您使用(other.bs)作为初始化程序,并且目标类型是数组,因此唯一适用的项目是我突出显示的项目。 The program is ill-formed. 该计划格式不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:数组初始化程序必须是初始化程序列表 - error: array initializer must be an initializer list C ++错误:数组初始化程序必须是初始化程序列表 - c++ error: array initializer must be an initializer list 数组初始化程序必须是初始化程序列表或字符串文字 - array initializer must be an initializer list or string literal C ++:为什么会出现错误:“数组初始化器必须是初始化器列表或字符串文字”? - C++: Why am I getting an error: “array initializer must be an initializer list or string literal”? 数组初始化程序必须是初始化程序列表或字符串文字,为什么? - Array initializer must be an initializer list or string literal why? C++:为什么我在实际使用字符串时收到错误消息:“数组初始值设定项必须是初始值设定项列表或字符串文字”? - C++: Why am I getting an error: "array initializer must be an initializer list or string literal" when I am in fact using a string? 从初始值设定项列表错误分配给数组 - Assigning to an array from an initializer list error 错误:从初始化列表分配给数组 - Error: Assigning to an array from an initializer list 初始化列表到数组 - Initializer list to array C++ 错误:“必须使用大括号括起来的初始化程序初始化数组” - C++ error: “Array must be initialized with a brace enclosed initializer”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM