How to suppress GCC compiler warning: inline variables are only available with -std=c++1z or -std=gnu++1z

Viewed 2580

I am using an inline global variable which works well for the purpose of it.

class MyClass {
public:
    void Func() {
    }
}

inline MyClass myClass;  // global inline variable

Above works well for my purpose but I get a warning when my code compiles on gcc with compiler below C++17. Following is the warning

warning: inline variables are only available with -std=c++1z or -std=gnu++1z

Question:
How can I suppress the warning on gcc?

I tried to suppress the warning by using a #pragma like below

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc++17-extensions"
inline MyClass myClass;
#pragma GCC diagnostic pop

Above #pragma technique works on clang, but looks like GCC to not understand the #pragma? I just want to brute force suppress the warning on GCC. How can I do that?

Looks like gcc warning options list does not even mention about this? https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

I am using gcc (GCC) 7.3.0

1 Answers

To understand why you cannot suppress this warning, look at what happens if the code is compiled with a compiler that does not support inline variables. (Inline variable support started with gcc 7.) Older versions of gcc process your code and spit out error: 'myClass' declared as an 'inline' variable. Not a warning, but an unsuppressible error. Hard stop; object code not produced.

Newer versions of gcc are able to be more understanding and helpful, but at the same time they have an obligation to maintain some degree of compatibility with older compilers. These newer compilers can recognize this C++17 feature, and it's been determined that ignoring "inline" downgrades the error to a warning (compilation does not necessarily need to stop). Furthermore, the message was given information about how to resolve this situation (assuming the code is correct). At the same time, this warning is still essentially the error produced by older versions of gcc, just given a makeover to make it more user-friendly. It cannot be suppressed any more than the old error could. Your choices are to write valid pre-17 code or to enable C++17 features.

Related