C++11 brace-init

 

int a{0}; // same as int a = 0; 
string s{"hello"}; // same as string s("hello"); 
string s2{s}; // copy construction, same as string s2(s); 
vector<string> vs{"alpha", "beta", "gamma"}; 
map<string, string> stars 
  { 
    {"Superman", "+1 (212) 333-4234"}, 
    {"Batman", "+1 (212) 434-5478"} 
  }; 
double * pd = new double[3] {0.5, 2.8, 12.84}; // impossible before C++11
class C 
{ 
  int x[4]; 
  public: 
  C() : x{0, 1, 2, 3} {} // impossible before C++11
};

Empty braces mean default initialization.

 

int n{}; // n is 0 
int *p{}; // p is nullptr 
double d{}; // d is 0.0 
char ca[10]{}; // all 10 chars are initialized to '\0' 
string s{}; // same as string s; 
int * pi = new int[5]{}; // all 5 ints are initialized to 0

http://www.informit.com/articles/article.aspx?p=1852519 

 

'programming > C++' 카테고리의 다른 글

Universal reference  (0) 2013.02.18
shared_ptr, unique_ptr  (0) 2013.02.18
std::async and std::future 쓸 때...  (0) 2013.02.18
constexpr  (0) 2013.02.18
Three Optimization Tips for C++  (0) 2013.02.18
Posted by 무한자전거
,