Reputation: 5211
I am taking a class next week on C++11/14 and need to verify that my design tools are up to date and can actually compile C++11/14.
What is the simplest piece of code that I can put together to verify that I can actually compile and execute C++11/14 code on my Linux box? I know I need GCC 4.9.X or better, but I want to just be sure that everything is jiving before I show up.
Thanks for the help.
Upvotes: 10
Views: 11376
Reputation: 31519
You can initially compile with -std=c++14
. If your gcc (or clang) is not c++14 compliant then compilation will fail (due to uknown flag) :
g++: error: unrecognized command line option '-std=c++14'
Regarding feature availability (querying the existence of specific features), you can perform feature testing. An example document can be found here : it all boils down to the use of macros to test the availability of the specified feature (therein you can also find what features to look for).
So even if you want to build with several compilers you can write code like this (naive example follows) :
#if __cpp_constexpr
constexpr
#endif
int triple(int k) { return 3*k; } // compiles with c++98 as well
which is how cross platform developing overcomes the joys of supporting multiple compilers and their versions (more elaborate examples would show supporting sth one way in gcc and another way in cl due to different speed of standard implementation)
Upvotes: 18
Reputation: 10998
Compiling with flag -std=c++11 or -std=c++14 as per Nikos answer can determine if the compiler supports these standards.
Small examples using C++11 and C++14:
For C++11, you could try compiling auto with -std=c++11:
#include <memory>
using namespace std;
int main()
{
auto p1 = make_shared<int>(42);
// use p1
}
For C++14, try using auto as return type with -std=c++14:
auto func(int i)
{
return [i=std::move(i)](int b){return b+i;};
}
int main()
{
int num = func(3)(5);
// use num
}
Upvotes: 3
Reputation: 719
To be sure it works you can simple try to compile some code available only since c++14. For example
#include <iostream>
#include <tuple>
#include <functional>
auto f() // this function returns multiple values
{
int x = 5;
return std::make_tuple(x, 7); // not "return {x,7};" because the corresponding
// tuple constructor is explicit (LWG 2051)
}
int main()
{
// heterogeneous tuple construction
int n = 1;
auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
n = 7;
std::cout << "The value of t is " << "("
<< std::get<0>(t) << ", " << std::get<1>(t) << ", "
<< std::get<2>(t) << ", " << std::get<3>(t) << ", "
<< std::get<4>(t) << ")\n";
// function returning multiple values
int a, b;
std::tie(a, b) = f();
std::cout << a << " " << b << "\n";
}
If this does work than you have the right setup :D Hope this helps
Upvotes: 4
Reputation: 10039
The __cplusplus
macro contains the C++ standard that the compiler is using. Each version of C++ has a specific value for this define. These may (counter intuitively) not match exactly with the name of the standard, so you can use gcc to determine what the values are. For example:
#include <stdio.h>
int main()
{
printf("%ld\n", __cplusplus);
}
Compiled like so:
g++ -std=c++98 file.cpp
g++ -std=c++11 file.cpp
g++ -std=c++14 file.cpp
Gives the following respectively when run:
199711
201103
201300
You can then use the predefined macros to generate errors if the value you're looking for isn't available. For example:
#if __cplusplus < 201300
#error I require C++14 for this code, so die.
#endif
// ...
Then g++ -std=c++11 file.cpp
will fail compilation.
Upvotes: 10