使用索引指示符初始化_C++_对象的数组

Initializing an array of _C++_ objects with index designators

本文关键字:C++ 对象 数组 初始化 索引 指示符      更新时间:2023-10-16

我正在尝试将一些代码移植到GCC,它可以用IAR编译器编译。该代码初始化一个C++对象数组(一个带有字符数组的结构)。我可以让它在C中使用GCC,但不能使用C++。这里有一个简单的例子。

#include  <stdio.h>
typedef struct
{
 int lineID[10];
} TMenu;
static const TMenu t1[8] =
{
    {{3}},
    {{4}},
    [6] = {{33, 22}},
    [8] = {{33, 22}},
    {{}},
    {{9,8,7,6,5,4,3,2,1}},
};

注意:我还必须在初始化项周围添加额外的花括号,IAR对此没有抱怨。

它用GCC编译得很好,但当用G++编译时,我会得到以下错误。

x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '._2 = {{33, 22}}'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '._3 = {{33, 22}}'

看起来GCC 4.7离支持这个构造越来越近了。以下是编译同一示例时GCC 4.5、4.6和4.7的输出。

GCC 4.5.3

$ /opt/local/bin/g++-mp-4.5 -Wall -std=c++0x -o y.exe x.c
x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '._2 = {{33, 22}}'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '._3 = {{33, 22}}'

GCC 4.6.3

$ /opt/local/bin/g++-mp-4.6 -Wall -std=c++0x -o y.exe x.c
x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '{} = {{33, 22}}'
x.c:12:20: note: candidate is:
x.c:12:7: note: <lambda()>&<lambda()>::operator=(const<lambda()>&) <deleted>
x.c:12:7: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const<lambda()>&'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '{} = {{33, 22}}'
x.c:13:20: note: candidate is:
x.c:13:7: note: <lambda()>&<lambda()>::operator=(const<lambda()>&) <deleted>
x.c:13:7: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const<lambda()>&'

GCC 4.7.0

$ /opt/local/bin/g++-mp-4.7 -Wall -std=c++0x -o y.exe x.c
x.c:16:1: sorry, unimplemented: non-trivial designated initializers not supported
x.c:16:1: sorry, unimplemented: non-trivial designated initializers not supported

我从未见过像这样的语法

[6] = {33, 22}},

在C++中。您可以尝试只使用空的初始值设定项来填补空白。以下关于ideone的作品:

#include <iostream>
struct TMenu
{
  int lineID[10];
};
int main(int, char*[])
{
  const TMenu t1[8] =
  {
    {{3}},
    {{4}},
    {{33, 22}},
    {{33, 22}},
    {{}},
    {{9, 8, 7, 6, 5, 4, 3, 2, 1}},
  };
  for (int i = 0; i < 8; ++i)
  {
    for (int j = 0; j < 10; ++j)
    {
      std::cout << t1[i].lineID[j] << "t";
    }
    std::cout << std::endl;
  }
}