0

Consider the following program

#include <iostream>

int main()
{
    std::string a = "Hello World";  // Copy initialization
    std::string b("Hello World");   // Direct initialization
    std::string c{"Hello World"};   // Value initialization
}

I realize that the results of these 3 lines are the same, but I wonder if they work differently under the hood. If there is such a difference, what is it? And which one of these initializations is the most efficient?

cuong.pq
  • 137
  • 1
  • 1
  • 4
  • `{ }` works the best or is preferred as it doesn't perform implicit conversions. [This](https://en.cppreference.com/w/cpp/language/initialization) should answer your doubts. –  Apr 17 '20 at 04:29
  • 5
    [C++ Initialization](https://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=initialization) – David C. Rankin Apr 17 '20 at 04:29
  • FWIW, the last one is aggregate initialization, not value initialization. `std::string c{};` will be value initialzation. See https://en.cppreference.com/w/cpp/language/aggregate_initialization and https://en.cppreference.com/w/cpp/language/value_initialization. – R Sahu Apr 17 '20 at 04:41

0 Answers0