Compiling with the flag -std=c++14
compiles programs which use features implemented in newer versions of C++ as well, issuing a warning like the following:
warning: inline variables are only available with -std=c++17 or -std=gnu++17
I don't want g++ to compile the program in this situation and do not know why it does in the first place.
I found out that adding the flag -Werror
converts the above warning to an error, ensuring that the program does not compile but I am not sure whether that is the recommended way to do so.
To provoke compiler errors specifically for the use of language features that are legal only in C++ standards later than your chosen one, the best-targeted diagnostic option is probably -pedantic-errors
, which is documented
-pedantic-errors
Give an error whenever the base standard (see -Wpedantic) requires a diagnostic , in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard...
[my emphasis]
The "base standard" here is the C++ standard named by the specified or default value of -std=...
(or if that one is a GNU dialect like gnu++14
, then it is the C++ standard on which that dialect is based).
If you compile source code with, say, std=c++14
that uses a construct first legalised in C++17, then that code is ill-formed per the C++14 standard and a diagnostic is required. The addition of -pedantic-errors
to -std=c++14
will therefore oblige the compiler to diagnose the C++17 innovations as errors.
Eg, without -pedantic-errors
$ cat foo.cpp
struct foo
{
inline static const int value = 42;
};
$ g++ -std=c++14 -Wall -Wextra -pedantic -c foo.cpp
foo.cpp:3:5: warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
3 | inline static const int value = 42;
| ^~~~~~
And with -pedantic-errors
$ g++ -std=c++14 -Wall -Wextra -pedantic-errors -c foo.cpp
foo.cpp:3:5: error: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
3 | inline static const int value = 42;
| ^~~~~~
-pedantic-errors
will make the compiler more fussy about C++14 conformity than std-c++14
by itself, or with -Werror
. But I guess you won't object to that. And you can make an unconstrained choice as to whether you also practice the blanket discipline of compiling with zero warnings ( -Werror
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.