1

Till now I have only seen the assignment initialization for POD arrays in C++. For instance,

int array[]={1,2,3};

From here I learned that it is possible to initialize an array using the initialization list approach when it is inside a class/struct in C++11. But I tried to define the array like:

int array[]({1,2,3});

An error occurred when compiling:

array initializer must be an initializer list

I think it only needs a few modifications but I just cannot figure out. Could you please tell me how can I achieve it?

BTW, is the copy constructor more efficient than the assignment one here? I guess not, but I don't know the exactly answer.

Community
  • 1
  • 1
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85

2 Answers2

3

POD arrays can't be initialized with constructor-like (parentheses) syntax. Use initializer list (curly brace) syntax to initialize variables if you want to initialize variables in a unified manner. For example, the following code will compile with gcc 4.7.3:

struct Foo
{
    Foo(int a, std::string s);
}

int main()
{
    // can also use parentheses on the next two lines
    int a{3};
    Foo f{a, "A string"};

    // only curly braces can be used here
    int arr[]{4, 5, 6};

    return 0;
}

The curly brace syntax also allows you to use std::initializer_list to construct vectors and other STL types in the same manner as you can for an array, which is only possible with parentheses like so:

std::vector<int> v{1, 2, 3, 4, 5};

// contrast with

std::initializer_list<int> i{1, 2, 3, 4, 5}; // must use curly braces here
std::vector<int> v(i);
masrtis
  • 1,282
  • 17
  • 32
  • But your example doesn't show the difference, curly brace can still be replaced with parentheses here. Also, there is no array initialization. – Hongxu Chen Jun 29 '13 at 14:40
  • I edited the post to show array initialization. Except for the exceptions @AndyProwl mentioned briefly in comments to his answer, curly brace initialization is equivalent to calling the constructor with parentheses. – masrtis Jun 29 '13 at 14:44
  • Got it, thanks(actually I am familiar with this form but have never thought the braces here). So the efficiency with `=` and `{}` are the same here? Maybe I should try to seem the generated assembly. – Hongxu Chen Jun 29 '13 at 14:50
  • Yes, the difference between the two forms is only in syntax; there should be no performance hit. – masrtis Jun 29 '13 at 14:53
2

Not sure I understand the context of your question correctly, but this is how you initialize an array in a constructor's initialization list:

struct X
{
    X() : arr{1, 2, 3} { }
//        ^^^^^^^^^^^^
private:
    int arr[3];
};
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Notice that this is C++11 syntax. In C++98 you can't initialize an array member. – peppe Jun 29 '13 at 14:10
  • @peppe: Right, but the question is tagged as C++11 – Andy Prowl Jun 29 '13 at 14:17
  • @peppe You can: `: arr()` Yes, I know that's not what you meant. :) –  Jun 29 '13 at 14:18
  • I am curious about whether array can be initialized the same way like those basic types like `int a(2);` so that the form of initialization can be unified. – Hongxu Chen Jun 29 '13 at 14:25
  • @HongxuChen: Vice versa, you can initialize other types with the braces, like: `X() : a{42}, arr{1, 2, 3}, Y{2} { ... }` – Andy Prowl Jun 29 '13 at 14:26
  • But I am not talking about the array type as a field of struct X, but the one defined inside a simple function such as `int main(void){int array[]({1,2,3});}` – Hongxu Chen Jun 29 '13 at 14:29
  • @HongxuChen: OK, even in that case, in general you can initialize other objects using braces rather than parentheses (there are a few exceptions though). That's the so-called "uniform initialization" – Andy Prowl Jun 29 '13 at 14:33
  • So it is impossible for array? – Hongxu Chen Jun 29 '13 at 14:41
  • @HongxuChen: Yep, that's what we are trying to tell you: if you want uniform initialization, use uniform initialization (with braces) :) – Andy Prowl Jun 29 '13 at 14:50