gcc/g++编译参数std支持不一样的标准

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++新版本中编译彻底没有问题

Ubuntu下安装并配置VS Code编译C++