Properly initialising variables in modern C++ (C++11 and above), using () or {}?

前端 未结 7 975

The C++ reference pages say that () is for value initialisation, {} is for value and aggregate and list initialisation. So, if I just want value initialisation, which one do

7条回答
  •  天命终不由人
    2020-12-13 18:43

    {} is value initialization if empty, if not it is list/aggregate initialization.

    From the draft, 7.1.6.4 auto specifier, 7/... Example,

    auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list

    Rules are a little bit complex to explain here (even hard to read from the source!).

提交回复
热议问题