C ++ 11 and generic initializer conventions

C ++ 11 brings new generic initializers that can be nice. Question. Is any old syntax for initializing objects considered deprecated. In C ++ 03, an object can be initialized as

  • Foo bar(x)
  • Foo bar=Foo(x)

Option (1) is preferred since it does not include a copy. There are several more ways in C ++ 11:

  1. Foo bar{x}
  2. auto bar=Foo{x}

Using move constructors and assignment operators, parameter (4) must also be accurate. Is (1) obsolete in favor of (3) or (4)?

In addition, in C ++ 03, the rule is that all constructors that take one argument must be explicit (except copy ctor). Other constructors are always explicit. With generic initializers, any ctor can be implicit. Is the new rule then explicit in all places or just where the conversion will mean side effects (allocation of a bunch of memory, creating a file ...) or be lossy (double to float)?

+4
source share
1 answer

Is (1) obsolete in favor of (3) or (4)?

, , . , . , Foo(x) Foo{x}.

std::vector:

std::vector<int> v1(10, 0); // Creates a vector of size 10 initialized with all 0's.
std::vector<int> v2{10, 0}; // Creates a vector with elements {10, 0}.

ctors; , , (1) (3) (4).

:

, explicit ...?

. , , , explicit.

+3

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


All Articles