C++ standard version (-cpp-version)
Specify C++ language standard followed in source code
Description
Specify the C++ language standard that you follow in your source code. This option applies to both dynamic testing and static analysis.
Set Option
Set the option using one of these methods:
Polyspace Platform user interface (desktop products only): In your project configuration, on the Build tab, select the Target & Compiler node and then select a value for this option. See Dependencies for other options you must enable first.
Command line and options file: Use the option
-cpp-version. See Command-Line Information.
Why Use This Option
Use this option so that Polyspace® can allow features from a specific version of the C++ language standard during compilation. For instance, if you compile with GCC using the flag -std=c++11 or -std=gnu++11, specify cpp11 for this option. If you are not sure of the language standard, specify defined-by-compiler.
For instance, suppose you use range-based for loops. This type of for loop is defined in the C++11 standard but unrecognized in prior standards such as C++03. If the Polyspace compilation uses the C++03 standard, you can see compilation errors.
To check if your compiler allows features specific to a standard, compile code with macros specific to the standard using compiler settings that you typically use. For instance, to check for C++11-specific features, compile this code. The code contains a C++11-specific keyword nullptr. If the macro __cplusplus is not 201103L (indicating C++11), this keyword is used and causes a compilation
error.
#if defined(__cplusplus) && __cplusplus >= 201103L
/* C++11 compiler */
#else
void* ptr = nullptr;
#endif
cpp11 for this option.Settings
Default: defined-by-compiler
defined-by-compilerThe analysis uses a standard based on your specification for
Compilation toolchain (Static analysis).cpp03The analysis uses the C++03 Standard (ISO®/IEC 14882:2003).
cpp11The analysis uses the C++11 Standard (ISO/IEC 14882:2011).
cpp14The analysis uses the C++14 Standard (ISO/IEC 14882:2014).
cpp17The analysis uses the C++17 Standard (ISO/IEC 14882:2017).
cpp20The analysis uses the C++20 Standard (ISO/IEC 14882:2020).
Dependencies
This option is available only if you set
Source code language (-lang)toCPPorC-CPP.If you create a project or options file from your build system using the
polyspace-configurecommand orpolyspaceConfigurefunction, the value of this option is automatically determined from your build system.If the build system uses different standards for different files, the subsequent Polyspace analysis can emulate your build system and use different standards for compiling those files. If you open such a project in the Polyspace user interface, the option value is shown as
defined-by-compiler. However, instead of one standard, Polyspace uses multiple standards for compiling the files. The analysis uses the hidden option-options-for-sourcesto associate different standards with different files.
Command-Line Information
Parameter: -cpp-version |
Value: defined-by-compiler | cpp03 | cpp11 | cpp14 | cpp17 |
Default:
defined-by-compiler |
Example (Bug Finder):
polyspace-bug-finder -lang c -sources |
Example (Code Prover):
polyspace-code-prover -lang c -sources |
Example (Bug Finder Server):
polyspace-bug-finder-server -lang c -sources |
Example (Code Prover Server):
polyspace-code-prover-server -lang c -sources |
Tips
This options helps Polyspace analyze version specific features during the compilation phase. It does not activate or deactivate coding rules associated with versions of the language.
If you specify earlier versions of the language using this option (such as C++03), Polyspace can still report violations of coding rules that are intended for later versions. For example, coding rules from the MISRA C++:2023 standard that concern C++11 or C++17 can be violated by code that use C++03 language version. Polyspace reports these violations regardless of what value you specify for
-cpp-version.