Why can I initialize a simple array using a list without using the assignment operator

I find in some code that we can initialize a simple c style array in C ++ using a list, without actually using the assignment operator. The code looks like this:

int a[4]{1, 2, 3, 4};
for (int i =0; i < 4; i++){
    cout << a[i] << '\n';
}

      

The above code works correctly. The above initialization is similar to how we initialize an int vector in C ++. I am trying to use this in C. But it is not allowed.

Why can we do this in C ++ but not C? Since when can we use this initialization syntax for a simple C ++ array? Can you provide any links to this syntax? I don't know where to find it.

+3


source to share


3 answers


Why can we do this in C ++ but not C?

C ++ and C are different languages. In C is required =

. In C ++ (since C ++ 11) this is optional.

In C ++ it is called aggregate initialization , which has the following syntax:



T object = {arg1, arg2, ...};
T object {arg1, arg2, ...};     // (since C++11)

      

For more information see section 8.5.1 Aggregates [dcl.init.aggr] in the C ++ specification.

+2


source


In C, initialization fixed one form

init-declarator:
    declarator
    declarator = initializer 
              ^^^

      

There are various forms of initialization in C ++ with different semantics.

In C ++, initialization is subdivided into initialization by semantic semantics, semantics, semantics, semantics, and direct initialization.

For example, these initializations for non-aggregate type A

A a = { 10 }; // copy-initialization

      

and

A a { 10 }; // direct-initialization

      

may give different results depending on whether the corresponding constructor is explicit or implicit.



Consider the following program

int main()
{
    struct A
    {
        explicit A(int x) : x(x) {}

        int x;
    };

    A a1 { 1 };
    // A a2 = { 1 }; - compilation error
}

      

In addition, there are also initializer lists and initializer list constructors in C ++.

Modify the previous demo program as follows.

#include <iostream>
#include <initializer_list>
#include <numeric>

int main()
{
    struct A
    {
        explicit A(int x) : x(x) 
        {
            std::wcout << "A::A( int )" << std::endl;
        }
        explicit A(std::initializer_list<int> lst) :
            x(std::accumulate(lst.begin(), lst.end(), 0)) 
        {
            std::wcout << "A::A( std::ininitializer_list<int> )" << std::endl;
        }
        int x;
    };

    A a { 1 };
}

      

and see its output.

There is no such partition in C. It doesn't make sense because there is no special initialization semantics behind it in C.

Introducing various forms of initialization without special semantics will only confuse users.

+1


source


This is part of the relatively new C ++ - 11 standard. This was not the case before. pre-c11 c ++ gives an error message there. Well, "c" is a much older standard :)

$ g++ a.cpp
a.cpp:3:16: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  static int a[4]{1,2,3,4};

      

-4


source







All Articles