C++ 在初始化列表中初始化一个常量大小的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11837078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:36:51  来源:igfitidea点击:

Initialize a constant sized array in an initializer list

c++arraysclassconstantsinitializer-list

提问by Serge

I've got a situation which can be summarized in the following:

我有一个情况,可以总结如下:

class Test
{

    Test();

    int MySet[10];

};

is it possible to initialize MySetin an initializer list?

是否可以MySet在初始化列表中进行初始化?

Like this kind of initializer list:

像这种初始化列表:

Test::Test() : MySet({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {}

Is there any way to initialize a constant-sized member array in a class's initalizer list?

有没有办法在类的初始化器列表中初始化一个常量大小的成员数组?

回答by oldrinb

While not available in C++03, C++11 introduces extended initializer lists. You can indeed do it if using a compiler compliant with the C++11 standard.

虽然在 C++03 中不可用,但 C++11 引入了扩展初始化列表。如果使用符合 C++11 标准的编译器,您确实可以做到。

struct Test {
    Test() : set { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } { };
    int set[10];
};

The above code compiles fine using g++ -std=c++0x -c test.cc.

上面的代码使用g++ -std=c++0x -c test.cc.



As pointed out below me by a helpful user in the comments, this code does not compileusing Microsoft's VC++ compiler, cl. Perhaps someone can tell me if the equivalent using std::arraywill?

正如一位乐于助人的用户在评论中指出的那样,此代码使用 Microsoft 的 VC++ 编译器 cl 进行编译。也许有人可以告诉我是否等效使用std::array

#include <array>

struct Test {
  Test() : set { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } } { };
  std::array<int, 10> set;
};

This also compiles fine using g++ -std=c++0x -c test.cc.

这也可以使用g++ -std=c++0x -c test.cc.

回答by John Humphreys - w00te

Unfortunately, in C++03, you cannot initialize arrays in initializer lists. You can in C++11 though if your compiler is newer :)

不幸的是,在 C++03 中,您不能在初始化列表中初始化数组。如果您的编译器更新,您可以在 C++11 中使用 :)

see: How do I initialize a member array with an initializer_list?

请参阅:如何使用 initializer_list 初始化成员数组?

回答by Cheers and hth. - Alf

"I understand that Set is just a pointer to the static array of 10 integers"

“我知道 Set 只是一个指向 10 个整数的静态数组的指针”

No, that's wrong: it's an array, not a pointer.

不,这是错误的:它是一个数组,而不是一个指针。

You can still initialize it in the constructor's initializer list.

您仍然可以在构造函数的初始化列表中对其进行初始化。

For a compiler that doesn't support C++11 curly braces initialization (Visual C++ version 11 and earlier comes to mind) you'll have to jump through some hoops though, as shown below:

对于不支持 C++11 花括号初始化的编译器(想到 Visual C++ 版本 11 及更早版本),您将不得不跳过一些障碍,如下所示:

#include <iostream>
#include <vector>
using namespace std;

#define CPP11
#if defined( _MSC_VER )
#   if (_MSC_VER <= 1700)
#       undef CPP11
#   endif
#endif

#ifdef CPP11
class Cpp11
{
private:
    int set_[10];

public:
    Cpp11()
        : set_{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    {}

    int foo() const { return set_[3]; }
};
#endif

class Cpp03
{
private:
    struct IntArray10 { int values[10]; };
    IntArray10 set_;

    static IntArray10 const& oneToTen()
    {
        static IntArray10 const values =
            { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} };
        return values;
    }

public:
    Cpp03()
        : set_( oneToTen() )
    {}

    int foo() const { return set_.values[3]; }
};

int main()
{}

Instead of using raw arrays, though, use std::vectorand C+++11 std::array, both of which are supported even by Visual C++ 11.

但是,不要使用原始数组,而是使用std::vector和 C+++11 std::array,即使 Visual C++ 11 也支持这两者。