How To Use C++ Standards In C++ Compiler Options
C++ is a very decent programming language that has a very strong compiler supported by a big community on a range of different platforms. The C++ language definitions, syntax, and functionality are organized into different standards. Those standards are usually named after the year the standard was adopted such as 1998 for C++98, 2011 for4 C++11, 2014 for C++14, and 2017 for C++17. One of the great features of a C++ compiler is you can choose which standards you want your code to be compiled against, before the compilation of your source code. This allows the compiler to check that your code complies with that standard. In this post, we explain what the standards are, how you can view them, how you can check the compatibility of your C++ source code against different standards, and how you can choose to use C++ standards in C++ compiler options. What is a C++ standard? Standards are an international agreement for C++ compilers, made by the IDE and compiler developers of different operating systems (Embarcadero C++ Builder, Microsoft MSVC, GNU GCC, Apple Swift, etc.). They are formal, legal, and very high-level detailed technical documents intended primarily for people writing C++ compilers and standard library implementations. The current available standards and some of their key features are listed below. How to use C++ standards in C++ compiler options? When we use ‘-std=’ option in a C++ compiler this option type enables or disables some features. We need to use these features to see the effect of each C++ standard option. Personally, I always prefer to use the latest version of the standards in my compilers. However, when programming, sometimes you may have to work on old or legacy C++ source, or you may be programming to another company that uses older standard compilers. Or maybe you just want to check if your code is compatible with a particular standard, or the code uses new standards. At these times, you can use std option in C++ compilers. In general, we can use the -std option to compile to a particular C++ standard. Here are some option examples, -std=c++98 -std=c++11 -std=c++14 -std=c++17 -std=c++20 or -std=c++2a For example, if you want to compile your code with C++11 features, you can use C++ Builder 64bits command line compiler as shown below: bcc64 –std=c++11 myapp.cpp –o myapp For an example, we can write this C++ code below which uses a feature found in the C++17 standard and above. int main() { static_assert(sizeof(int)==4); // C++17 feature return 0; } If you are using the C++17 compiler, this code will be compiled successfully because the static_assert feature comes with C++17. If you compile this code with -std=c++11 or -std=c++14 options this time you will get warning which is telling you that you have requested C++ standard version 11, but there is a feature that requires C++ standard version 17 (Until C++17, a message was required w/ static_assert). How to use C++ standards in the C++ Builder compiler options? In C++ Builder CLANG compiler, we can use -std option to compile in previous standards. This option can be used in bcc32c and bcc64, both support these std options below, -std=c++11 -std=c++14 -std=c++17 I should note that, C++11 and C++14 options are partially supported, these options do not have a STL that works with all, so you have the latest STL one for C++17. According […]
