// Test new c++ language features. // compile with -std=c++11 // or at least -std=c++0x // References: c++11 features: // http://en.wikipedia.org/wiki/C%2B%2B11 // http://www.research.ibm.com/arl/seminar/media/stroustrup.pdf // References: // matrix showing feature status for various vendors: // http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport // matrix showing feature status for various g++ versions: // http://gcc.gnu.org/projects/cxx0x.html // // Note: oneiric uses gcc 4.6 // natty uses gcc 4.5 // natty can be upgraded to 4.6 with minimal effort // Hint: "pinning" as discussed at http://wiki.debian.org/AptPreferences //>> cat /etc/apt/preference.d/foo //>> Package: * //>> Pin: release a=natty //>> Pin-Priority: 900 //>> //>> Package: * //>> Pin: release a=oneiric //>> Pin-Priority: 800 // Hint: list of common pre-defined macros: // http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html // To see the names (and values) of macros that are actually pre-defined: // cat /dev/null > foo.c // g++ -std=c++11 -dM -E foo.c > foo.dm #include #include #include #include using namespace std; #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6 #define SHOULD_HAVE_NULLPTR #endif string foo(const int){ return "int"; } string foo(const int* ptr){ if (ptr == 0) return "nullptr"; return "generic int*"; } size_t foo(vector v){ return v.size(); } void dummy(void*){} int main() { ////////////////////////////////////////////////// // Feature: lambda expressions (i.e. anonymous functions) int bar = [](int x){return 1+x;}(3); assert (bar == 4); // Feature: decltype decltype(bar) bar2 = 2*bar; assert (bar2 == 8); ////////////////////////////////////////////////// // Feature: "auto" type declaration: int explicit_paren(1+2); // should be an int auto auto_paren(2+3); // should be an int int explicit_uniform{3+4}; // should be an int auto auto_uniform{5+6}; // apparently a one-element /list/ of ints assert(auto_uniform.size() == 1); ////////////////////////////////////////////////// // Feature: Uniform Initialization auto stuff{2, explicit_paren, auto_paren, explicit_uniform}; assert(sizeof(stuff) == 4*sizeof(int)); assert(stuff.size() == 4); // uniform initialization of named vector: vector vint{2, 3, 5, 7}; assert (vint.size() == 4); // uniform initialization of anonymous ephemeral vector: int anonsize{vector{2, 3, 5, 7}.size()}; assert (anonsize == 4); // uniform initialization of function argument, promoted to vector: assert (foo({5, 10, 15, 20}) == 4); //xx Fails because auto_uniform is not an int, //xx but rather a one-element list. //xx Error msg tells us that //xx auto_uniform will be of type "std::initializer_list" //xx int fail[]{2, 3, auto_uniform}; // This error msg tells us that // nullptr has type "std::nullptr_t" //xx int play[]{2, 3, nullptr}; ////////////////////////////////////////////////// // Feature: nullptr #ifdef SHOULD_HAVE_NULLPTR assert (foo(nullptr) == "nullptr"); #else cerr << "Sorry, nullptr (probably) not implemented yet." << endl; #endif ////////////////////////////////////////////////////////////////////// cout << "All OK!" << endl; }