What do empty curly braces mean in a structure declaration?

The following is an abbreviated definition structshowing only the problem.

struct Entry {
    // bookkeeping record for managing solution search
    std::array<std::array<bool, DIM>, DIM> filled;   // which holes have been filled
    std::array<std::array<char, MAX>, MAX> cells;    // individual cell entries, 0=empty
    std::vector<Constraint> overlaps;
    std::vector<Hole*>::iterator candidates;
    Entry() = default;
};

It was a mistake. I thought the default constructor would be zero to initialize arrays, but it just fills them with random garbage. Now I know that I need to write a default constructor, but I am confused by the behavior that I encountered when testing.

This is a shortened version of my test function:

void testEntry(void) {
    Entry e;
    std::cout << std::boolalpha;
    e.cells[1][2] = 'a';
    e.filled[0][0] = true;
    for (int i = 0; i < MAX; ++i)
        for (int j = 0; j < MAX; ++j)
            std::cout<<i<<" "<<j<<" "<<e.cells[i][j]<<std::endl;
    for (int i = 0; i < DIM; ++i)
        for (int j = 0; j < DIM; ++j)
            std::cout<<i<<" "<<j<<" "<<e.filled[i][j]<<std::endl;
}

When I ran it, arrays filledand cellscontain random garbage, and eventually I found my mistake. During debugging, I changed the declaration Entry e;to Entry e{};, and the changed code works as I expected, but I don’t understand why.

. , : " braced-init , T - , ." , , , . , zero-intialization " T - , , . , , ", .

, , . ( clang Xcode 7.2.1).

+4
2

?

T object {}; (4). .

, " ". .

, , , .

:

2), T - , , ( , ), , ; ( ++ 11)

. .

,

, . .

+6

++ 11 ++ 14, , .


, ( ) . ++ 11 [dcl.init.list]/3:

- T :

  • , T - , .

++ 14 :

- T :

  • T ,

Entry , ([dcl.init.aggr]/1):

- , (12.1), ,

"" .

[dcl.init.aggr], , . , , [dcl.init.aggr]/7:

-, , , , -- , - , .

, , , e.overlaps , std::vector<Constraint> overlaps{};.

, . -, (, std::vector), [dcl.init.list]/3, ++ 11: .


++ 11 ++ 14, : ++ 14 . . ++ 14 Entry . ( std::array ). , std::vector , ; , .

+1

Source: https://habr.com/ru/post/1654229/


All Articles