gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:16 26 4
gpt4 key购买 nike

如何通过参数在构造函数中初始化一个数组?我认为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com