2

Is it recommended in C++ 20 to always favor direct-list-initialization over copy initialization or even direct initialization as a general rule of thumb? As an old C++98 programmer coming back to C++ after 15 years it still feels natural to use auto i = 5 instead of auto i {5}. I want to make sure the "new" way really is the new default before "burning in" the new way into my coding guides.

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • 1
    Any sane compiler will generate the same code for both variants. – Jabberwocky Nov 29 '20 at 13:25
  • Afaik the second declaration leads `i` to be a `std::initializer_list`. Read [Herb Sutters GOTW 92](https://herbsutter.com/2013/06/07/gotw-92-solution-auto-variables-part-1/) – JHBonarius Nov 29 '20 at 13:44
  • Also `std::vector v(20,10)` does something different then `std::vector v{20,10}`. Read the [abseil TotW 88](https://abseil.io/tips/88) – JHBonarius Nov 29 '20 at 13:47
  • 2
    You may be interested in [The Nightmare of Initialization in C++](https://www.youtube.com/watch?v=7DTlWPgX6zs), presented by Nicolai Josuttis. My guidance is to favor readability and consistency (both for the sake of maintainability), in whatever style you choose. – Eljay Nov 29 '20 at 13:50
  • @JHBonarius: afaik the second declaration does no longer lead to an std::initializer_list in C++17. I've found https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-list which says I should favor it, and it looks like a somewhat "official" guideline to me. So I'm guessing it's best to go for it nowadays. – D.R. Nov 29 '20 at 14:22
  • @Eljay: Great video so far, C++ is great, but you can really feel it's legacy weight nowadays. – D.R. Nov 29 '20 at 14:23
  • 1
    The legacy weight is due to the nigh obsessive goal for backwards compatibility. I expect that in the future, there will be some sort of `extern "C++27" {...` marker to specify an area that uses newer, stricter syntax that does not allow some backwards compatible constructs. Like **JavaScript** and `'use strict'` or **D** and `@safe`. – Eljay Nov 29 '20 at 14:54
  • *To* the extent that your question is not opinion-based, does this answer it? [What are the differences between C-like, constructor, and uniform initialization?](https://stackoverflow.com/questions/24953658/what-are-the-differences-between-c-like-constructor-and-uniform-initialization) – Davis Herring Nov 29 '20 at 17:25
  • 1
    @JHBonarius `auto i{5};` declares an `int` (since C++14). `auto j = {5};` declares a `std::initializer_list`. – Barry Nov 29 '20 at 18:13
  • @Barry :-( I should stop reading dated blogs... thanks for the correction. – JHBonarius Nov 30 '20 at 09:14

1 Answers1

0

I suppose post modern C++ enables you to express "intent" instead of "operation". In that sense auto i {5}; would state you allow the compiler to choose the type and that you only care about 'i' being initialised, not caring how that happens? But if your intent is to initialise in some specific way you have chosen to be "the best" in some sense, then choose the initialisation you think comes closest to that way :)