4

I came across the following code

#include <iterator>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

int main() {

    std::string inputfilename, outputfilename;

    std::cin  >> outputfilename;

    std::ofstream outputfile{ outputfilename };

    outputfile << "I exist Yo!";

    return 0;
}

My first reaction was that it should not compile. I had never seen the outputfile{ outputfilename }; syntax. Can someone please tell me what feature of the C++ language defines the behavior of {...} in this line of code?

P.S. The code works and does what you would expect.

leemes
  • 44,967
  • 21
  • 135
  • 183
The Vivandiere
  • 3,059
  • 3
  • 28
  • 50

1 Answers1

8

From the C++11 Standard (emphasis mine):

8.5.4 List-initialization [dcl.init.list]

1 List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list. An initializer list may be empty. List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization. [ Note: List-initialization can be used

  • as the initializer in a variable definition (8.5)
  • as the initializer in a new expression (5.3.4)
  • in a return statement (6.6.3)
  • as a function argument (5.2.2)
  • as a subscript (5.2.1)
  • as an argument to a constructor invocation (8.5, 5.2.3)
  • [..]
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • What advantage does using list-initialization have (here)? – user2864740 Mar 03 '15 at 17:47
  • 3
    List-initialization was introduced in C++11 to overcome some of the challenges in initializing objects in C++03. See the [Uniform initialization](http://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization). – R Sahu Mar 03 '15 at 17:50
  • 2
    @ user2864740: List-initialization syntax happens to be that common syntax that works in all initialization contexts. In a *specific* context its advantages might not be obvious (yet they exist), but once you take the bigger picture into account, the global *uniformity* of the syntax alone is already a huge advantage. – AnT stands with Russia Mar 03 '15 at 17:59
  • How many books do I have to read before I could read and understand the standard by itself? – The Vivandiere Mar 03 '15 at 22:22
  • @user3670482, it's a combination of reading the standard, reading commentary on the standard, reading books, writing a lot of code, and learning from each as you go. – R Sahu Mar 03 '15 at 22:26
  • @RSahu Thanks. What do you recommend doing first? Reading advanced books like Effective C++, Effective Modern C++, or reading the standard? Or do you recommend I do both in unison? – The Vivandiere Mar 03 '15 at 22:27
  • @user3670482, take a look at http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. – R Sahu Mar 03 '15 at 22:35