gnu c++6.3已經支持c++17標準html
/* * * g++ -std=c++1z hello.cpp -o hello * g++ -std=c++17 hello.cpp -o hello * g++ -std=gnu++17 hello.cpp -o hello */ #include <iostream> #include <string> // a struct to get the first word of a string struct FirstWord { std::string data; // declare a predicate to make ' ' a string ender struct EndOfString { bool operator()(std::string::iterator it) { return (*it) != '\0' && (*it) != ' '; } }; std::string::iterator begin() { return data.begin(); } EndOfString end() { return EndOfString(); } }; // declare the comparison operator bool operator!=(std::string::iterator it, FirstWord::EndOfString p) { return p(it); } // test int main() { for (auto c : {"Hello World !!!"}) std::cout << c; std::cout << std::endl; // print "Hello World !!!" for (auto c : FirstWord{"Hello World !!!"}) // works with gcc with C++17 enabled std::cout << c; std::cout << std::endl; // print "Hello" }
編譯運行:(-std=c++1z或-std=gnu++17或-std=c++17)ios
$ g++ -std=c++1z -o hello hello.cpp $ ./hello Hello World !!! Hello
ISO C++14 標準用:-std=c++14或-std=c++1y或-std=gnu++14c++
ISO C++11 標準用默:(或-std=c++11或-std=gnu++11)spa
最新的g++6.3已經默認支持c++14c++11
c++11的例子:code
/* * * g++ -std=c++11 hello11.cpp -o hello11 * g++ -std=gnu++11 hello11.cpp -o hello11 * g++ hello11.cpp -o hello11 * */ #include <iostream> void tprintf(const char* format) // base function { std::cout << format; } template<typename T, typename... Targs> void tprintf(const char* format, T value, Targs... Fargs) // recursive variadic function { for ( ; *format != '\0'; format++ ) { if ( *format == '%' ) { std::cout << value; tprintf(format+1, Fargs...); // recursive call return; } std::cout << *format; } } int main() { tprintf("% world% %\n","Hello",'!',123); return 0; }
c++14的例子: orm
/* * * g++ -std=c++14 14test.cpp -o 14test * g++ -std=gnu++14 14test.cpp -o 14test * g++ -std=c++1y 14test.cpp -o 14test */ #include<iostream> #include<complex> int main() { // Store a generalized lambda, that squares a number, in a variable auto func = [](auto input) { return input * input; }; // Usage examples: // square of an int std::cout << func(10) << std::endl; // square of a double std::cout << func(2.345) << std::endl; // square of a complex number std::cout << func(std::complex<double>(3, -2)) << std::endl; return 0; }
又一例:htm
#include<iostream> #include<vector> #include<numeric> #include<algorithm> int main() { std::vector<int> V(10); // Use std::iota to create a sequence of integers 0, 1, ... std::iota(V.begin(), V.end(), 1); // Print the unsorted data using std::for_each and a lambda std::cout << "Original data" << std::endl; std::for_each(V.begin(), V.end(), [](auto i) { std::cout << i << " "; }); std::cout << std::endl; // Sort the data using std::sort and a lambda std::sort(V.begin(), V.end(), [](auto i, auto j) { return (i > j); }); // Print the sorted data using std::for_each and a lambda std::cout << "Sorted data" << std::endl; std::for_each(V.begin(), V.end(), [](auto i) { std::cout << i << " "; }); std::cout << std::endl; return 0; }
固然c++11的程序用-std=c++14或-std=c++17編譯也是能經過的,反過來不行。blog
g++/gcc支持的標準參數:ci
-std=c++1z -std=f2003 -std=gnu11 -std=gnu90 -std=iso9899:1999 -std=c++03 -std=c89 -std=f2008 -std=gnu++11 -std=gnu++98 -std=iso9899:199x -std=c11 -std=c90 -std=f2008ts -std=gnu++14 -std=gnu99 -std=iso9899:2011 -std=c++11 -std=c++98 -std=f95 -std=gnu1x -std=gnu9x -std=legacy -std=c++14 -std=c99 -std=gnu -std=gnu++1z -std=iso9899:1990 -std=c1x -std=c9x -std=gnu++03 -std=gnu89 -std=iso9899:199409
例子:(複數)
#include <math.h> #include <stdio.h> #include <complex.h> int main(void){ double pi = 4 * atan(1.0); double complex z = cexp(I * pi); printf("%f + %f * i\n", creal(z), cimag(z)); }
上面的代碼在g++5.X如下能夠直接編譯,對於6以上版本需加上-std=c++03或-std=c++98才能夠
而
#include <iostream> #include <complex> using namespace std; int main(int argc, char *argv[]) { complex<double> a(1, 2), b(3, 4); cout << "a = " << a << ", b = " << b << endl; cout << "a + b = " << a + b << endl; cout << "a * b = " << a * b << endl; cout << "\nthe real part of a is " << real(a) << endl; cout << "the imaginary part of b is " << imag(b) << endl; cout << "the magnitude of a is " << abs(a) << endl; cout << "\ne^a = " << exp(a) << endl; cout << "ln(b) = " << log(b) << endl; return 0; }
在g++新版本中編譯徹底沒有問題