abseil / Tip of the Week #88 : Initialization : =, (), and {}

/88

  • Tip of the Week #88: Initialization: =, (), and {}
    https://abseil.io/tips/88

    Originally posted as TotW #88 on Jan 27, 2015

    by Titus Winters (titus@google.com), on behalf of the Google C++ Style Arbiters

    C++11 provided a new syntax referred to as “uniform initialization syntax” that was supposed to unify all of the various styles of initialization, avoid the Most Vexing Parse, and avoid narrowing conversions. This new mechanism means we now have yet another syntax for initialization, with its own tradeoffs.

    C++11 Brace Initialization

    Some uniform initialization syntax proponents would suggest that we use s and direct initialization (no use of the ‘=’, although in most cases both forms call the same constructor) for initialization of all types:

    int x2; std::string foo"Hello World"; std::vectorint> v1, 2, 3;

    vs. (for instance):

    int x = 2; std::string foo = "Hello (...)