[C++]C++11新特性

0. C++11 compiler support shootout

http://cpprocks.com/c11-compiler-support-shootout-visual-studio-gcc-clang-intel/
http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx

1. Important Minor Syntax Cleanups

//Spaces in Template Expressions
std::vector<std::list<int>>;

//nullptr and std::nullptr_t
f(0); //calls f(int)
f(NULL); //calls f(int) if NULL is 0,ambiguous otherwise
f(nullptr); //calls f(void*)

2. Automatic Type Deduction with auto

auto i = 42; // i has type int
//
std::vector<std::string> v;
auto pos = v.begin(); // std::vector<std::string>::iterator
//
auto func = [](int x)->bool {
    //....
}; // the type of lambda taking an int and returnin a bool

3. Uniform Initialization and Initializer Lists

/* not support in vs2012 */
int values[] { 1, 2,3 };
std::vector<int> v {2, 3, 5, 7,11, 13, 17};
std::vector<std::string> cities {
"Berlin", "New York", "London", "Braunschweig", "Cairo", "Cologne"
};

//std::initializer_list<>
class P {
public:
    P(int,int);
    P(std::initializer_list<int>);
};
//
P p(77,5); //calls P::P(int,int)
P q{77,5}; //calls P::P(initializer_list)
P r{77,5,42}; //calls P::P(initializer_list)
P s={77,5}; //calls P::P(initializer_list)

4. Range-Based for Loops

/*
for ( decl :coll ){
    statement
}
*/
std::vector<int> v;
for(auto& item : v) {
    //....
}

posted @ 2013-10-19 16:18  LambdaTea  阅读(390)  评论(0编辑  收藏  举报