c++ - 如何通过参数在构造函数中初始化数组?

标签 c++ c++11 gcc

如何通过参数在构造函数中初始化一个数组?我认为 type (&name)[size] 语法非常好,编译器维护者可以很容易地实现它。标准中是否有禁止此类初始化的段落?

代码:

struct Test
{
    char characters_[3];
    Test(char (&characters)[3]) : characters_(characters) {}
};

海湾合作委员会版本:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

GCC 输出:

FAILED: /usr/bin/c++     -Wall -Wextra -Wconversion -pedantic -Wmissing-declarations -Wmissing-include-dirs -Wfloat-equal -std=c++11 -pg   -m32 -std=gnu++11 -MMD -MT CMakeFiles/Test.dir/test.cpp.o -MF CMakeFiles/Test.dir/test.cpp.o.d -o CMakeFiles/Test.dir/test.cpp.o -c /home/user/Desktop/programms/test/Test/test.cpp
In file included from /home/user/Desktop/programms/test/Test/test.cpp:1:0:
/home/user/Desktop/programms/test/Test/test.h: In constructor ‘Test::Test(char (&)[3])’:
/home/user/Desktop/programms/test/Test/test.h:38:54: error: array used as initializer
  Test(char (&characters)[3]) : characters_(characters) {}
                                                      ^
FAILED: /usr/bin/c++     -Wall -Wextra -Wconversion -pedantic -Wmissing-declarations -Wmissing-include-dirs -Wfloat-equal -std=c++11 -pg   -m32 -std=gnu++11 -MMD -MT CMakeFiles/Test.dir/main.cpp.o -MF CMakeFiles/Test.dir/main.cpp.o.d -o CMakeFiles/Test.dir/main.cpp.o -c /home/user/Desktop/programms/test/Test/main.cpp
In file included from /home/user/Desktop/programms/test/Test/main.cpp:4:0:
/home/user/Desktop/programms/test/Test/test.h: In constructor ‘Test::Test(char (&)[3])’:
/home/user/Desktop/programms/test/Test/test.h:38:54: error: array used as initializer
  Test(char (&characters)[3]) : characters_(characters) {}
                                                      ^

最佳答案

也许最简单的解决方案是使用 std::array 代替:

std::array<char, 3> characters;
Test(std::array<char, 3> characters) : characters(characters) {}

在 C++11 之前,人们会在构造函数的主体中复制参数数组:

Test(char (&characters)[3]) {
    std::copy(characters, characters + sizeof characters, this->characters);
}

或者会使用自定义的 std::array-like 包装器。


Is there a paragraph in the standard prohibiting such initialization?

肯定有(标准草案):

[dcl.init]/17 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. [snip]

  • (17.1) — If the initializer is a (non-parenthesized) braced-init-list or is = braced-init-list, the object or reference is list-initialized (8.6.4).

  • (17.2) — If the destination type is a reference type, see 8.6.3.

  • (17.3) — 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 8.6.2.

  • (17.4) — If the initializer is (), the object is value-initialized.

  • (17.5) — Otherwise, if the destination type is an array, the program is ill-formed.

  • [snip]

如您所见,只有大括号初始化列表、字符串文字(仅适用于 char 数组)或空初始化器对数组有效。如果目标是数组,则不允许使用数组的源类型。

关于c++ - 如何通过参数在构造函数中初始化数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44151330/

相关文章:

objective-c - 如何在 Linux 中运行简单的 Objective-C

无输入编译

c++ - 继承CTAD构造函数

c++ - 在禁用 MSVC 语言扩展的情况下使用 Boost.Thread header

c++ - 基于范围的 for 循环等价物

c++ - 如何派生出需要嵌套类模板类型的模板类?

c - 为什么包含带有外部变量和函数的 h 文件会导致 undefined reference

c++ - GCC:仅 header 围绕 C++ 模板函数的 C 包装器(无链接器要求)

c++ - 作为抽象,C++ 是否支持 "bits"表示两个以上值之一?

c++ - 我们如何在指针中存储对对象的引用?