Next: Objective-C and Objective-C++ Dialect Options, Previous: C Dialect Options, Up: Invoking GCC
This section describes the command-line options that are only meaningful for C++ programs. You can also use most of the GNU compiler options regardless of what language your program is in. For example, you might compile a file firstClass.C like this:
g++ -g -fstrict-enums -O -c firstClass.C
In this example, only -fstrict-enums is an option meant only for C++ programs; you can use the other options with any language supported by GCC.
Some options for compiling C programs, such as -std, are also relevant for C++ programs. See Options Controlling C Dialect.
Here is a list of options that are only for compiling C++ programs:
-fabi-version=nVersion 0 refers to the version conforming most closely to the C++ ABI specification. Therefore, the ABI obtained using version 0 will change in different versions of G++ as ABI bugs are fixed.
Version 1 is the version of the C++ ABI that first appeared in G++ 3.2.
Version 2 is the version of the C++ ABI that first appeared in G++ 3.4, and was the default through G++ 4.9.
Version 3 corrects an error in mangling a constant address as a template argument.
Version 4, which first appeared in G++ 4.5, implements a standard mangling for vector types.
Version 5, which first appeared in G++ 4.6, corrects the mangling of attribute const/volatile on function pointer types, decltype of a plain decl, and use of a function parameter in the declaration of another parameter.
Version 6, which first appeared in G++ 4.7, corrects the promotion behavior of C++11 scoped enums and the mangling of template argument packs, const/static_cast, prefix ++ and –, and a class scope function used as a template argument.
Version 7, which first appeared in G++ 4.8, that treats nullptr_t as a builtin type and corrects the mangling of lambdas in default argument scope.
Version 8, which first appeared in G++ 4.9, corrects the substitution behavior of function types with function-cv-qualifiers.
Version 9, which first appeared in G++ 5.2, corrects the alignment of
nullptr_t.
Version 10, which first appeared in G++ 6.1, adds mangling of attributes that affect type identity, such as ia32 calling convention attributes (e.g. `stdcall').
Version 11, which first appeared in G++ 7, corrects the mangling of sizeof... expressions and operator names. For multiple entities with the same name within a function, that are declared in different scopes, the mangling now changes starting with the twelfth occurrence. It also implies -fnew-inheriting-ctors.
Version 12, which first appeared in G++ 8, corrects the calling conventions for empty classes on the x86_64 target and for classes with only deleted copy/move constructors. It accidentally changes the calling convention for classes with a deleted copy constructor and a trivial move constructor.
Version 13, which first appeared in G++ 8.2, fixes the accidental change in version 12.
Version 14, which first appeared in G++ 10, corrects the mangling of the nullptr expression.
Version 15, which first appeared in G++ 11, changes the mangling of
__alignof__ to be distinct from that of alignof, and
dependent operator names.
See also -Wabi.
-fabi-compat-version=nWith -fabi-version=0 (the default), this defaults to 11 (GCC 7 compatibility). If another ABI version is explicitly selected, this defaults to 0. For compatibility with GCC versions 3.2 through 4.9, use -fabi-compat-version=2.
If this option is not provided but -Wabi=n is, that
version is used for compatibility aliases. If this option is provided
along with -Wabi (without the version), the version from this
option is used for the warning.
-fno-access-control-faligned-newnew of types that require more
alignment than void* ::operator new(std::size_t) provides. A
numeric argument such as -faligned-new=32 can be used to
specify how much alignment (in bytes) is provided by that function,
but few users will need to override the default of
alignof(std::max_align_t).
This flag is enabled by default for -std=c++17.
-fchar8_t-fno-char8_tchar8_t as adopted for C++20. This includes
the addition of a new char8_t fundamental type, changes to the
types of UTF-8 string and character literals, new signatures for
user-defined literals, associated standard library updates, and new
__cpp_char8_t and __cpp_lib_char8_t feature test macros.
This option enables functions to be overloaded for ordinary and UTF-8 strings:
int f(const char *); // #1
int f(const char8_t *); // #2
int v1 = f("text"); // Calls #1
int v2 = f(u8"text"); // Calls #2
and introduces new signatures for user-defined literals:
int operator""_udl1(char8_t);
int v3 = u8'x'_udl1;
int operator""_udl2(const char8_t*, std::size_t);
int v4 = u8"text"_udl2;
template<typename T, T...> int operator""_udl3();
int v5 = u8"text"_udl3;
The change to the types of UTF-8 string and character literals introduces incompatibilities with ISO C++11 and later standards. For example, the following code is well-formed under ISO C++11, but is ill-formed when -fchar8_t is specified.
char ca[] = u8"xx"; // error: char-array initialized from wide
// string
const char *cp = u8"xx";// error: invalid conversion from
// `const char8_t*' to `const char*'
int f(const char*);
auto v = f(u8"xx"); // error: invalid conversion from
// `const char8_t*' to `const char*'
std::string s{u8"xx"}; // error: no matching function for call to
// `std::basic_string<char>::basic_string()'
using namespace std::literals;
s = u8"xx"s; // error: conversion from
// `basic_string<char8_t>' to non-scalar
// type `basic_string<char>' requested
-fcheck-newoperator new is non-null
before attempting to modify the storage allocated. This check is
normally unnecessary because the C++ standard specifies that
operator new only returns 0 if it is declared
throw(), in which case the compiler always checks the
return value even without this option. In all other cases, when
operator new has a non-empty exception specification, memory
exhaustion is signalled by throwing std::bad_alloc. See also
`new (nothrow)'.
-fconcepts-fconcepts-tsWith -std=c++20 and above, Concepts are part of the language
standard, so -fconcepts defaults to on. But the standard
specification of Concepts differs significantly from the TS, so some
constructs that were allowed in the TS but didn't make it into the
standard can still be enabled by -fconcepts-ts.
-fconstexpr-depth=n-fconstexpr-cache-depth=n-fconstexpr-fp-except constexpr float inf = 1./0.; // OK with -fconstexpr-fp-except
-fconstexpr-loop-limit=n-fconstexpr-ops-limit=n-fcoroutines-fno-elide-constructorsIn C++17, the compiler is required to omit these temporaries, but this
option still affects trivial member functions.
-fno-enforce-eh-specsNDEBUG. This does not give user code permission to throw
exceptions in violation of the exception specifications; the compiler
still optimizes based on the specifications, so throwing an
unexpected exception results in undefined behavior at run time.
-fextern-tls-init-fno-extern-tls-initthread_local and
threadprivate variables to have dynamic (runtime)
initialization. To support this, any use of such a variable goes
through a wrapper function that performs any necessary initialization.
When the use and definition of the variable are in the same
translation unit, this overhead can be optimized away, but when the
use is in a different translation unit there is significant overhead
even if the variable doesn't actually need dynamic initialization. If
the programmer can be sure that no use of the variable in a
non-defining TU needs to trigger dynamic initialization (either
because the variable is statically initialized, or a use of the
variable in the defining TU will be executed before any uses in
another TU), they can avoid this overhead with the
-fno-extern-tls-init option.
On targets that support symbol aliases, the default is
-fextern-tls-init. On targets that do not support symbol
aliases, the default is -fno-extern-tls-init.
-ffold-simple-inlines-fno-fold-simple-inlinesstd::move, std::forward,
std::addressof and std::as_const. In contrast to inlining, this
means no debug information will be generated for such calls. Since these
functions are rarely interesting to debug, this flag is enabled by default
unless -fno-inline is active.
-fno-gnu-keywordstypeof as a keyword, so that code can use this
word as an identifier. You can use the keyword __typeof__ instead.
This option is implied by the strict ISO C++ dialects: -ansi,
-std=c++98, -std=c++11, etc.
-fimplicit-constexpr-fno-implicit-templates-fno-implicit-inline-templates-fno-implement-inlines#pragma implementation. This causes linker
errors if these functions are not inlined everywhere they are called.
-fmodules-ts-fno-modules-ts-fmodule-header-fmodule-header=user-fmodule-header=system-fmodule-implicit-inline-fno-module-lazy-fmodule-mapper=[hostname]:port[?ident]-fmodule-mapper=|program[?ident] args...-fmodule-mapper==socket[?ident]-fmodule-mapper=<>[inout][?ident]-fmodule-mapper=<in>out[?ident]-fmodule-mapper=file[?ident]-fmodule-only-fms-extensions-fnew-inheriting-ctors-fnew-ttp-matching-fno-nonansi-builtinsffs, alloca, _exit,
index, bzero, conjf, and other related functions.
-fnothrow-optthrow() exception specification as if it were a
noexcept specification to reduce or eliminate the text size
overhead relative to a function with no exception specification. If
the function has local variables of types with non-trivial
destructors, the exception specification actually makes the
function smaller because the EH cleanups for those variables can be
optimized away. The semantic effect is that an exception thrown out of
a function with such an exception specification results in a call
to terminate rather than unexpected.
-fno-operator-namesand, bitand,
bitor, compl, not, or and xor as
synonyms as keywords.
-fno-optional-diags-fpermissive-fno-pretty-templatesvoid f(T) [with T = int]
rather than void f(int)) so that it's clear which template is
involved. When an error message refers to a specialization of a class
template, the compiler omits any template arguments that match
the default template arguments for that template. If either of these
behaviors make it harder to understand the error message rather than
easier, you can use -fno-pretty-templates to disable them.
-fno-rttidynamic_cast and typeid). If you don't use those parts
of the language, you can save some space by using this flag. Note that
exception handling uses the same information, but G++ generates it as
needed. The dynamic_cast operator can still be used for casts that
do not require run-time type information, i.e. casts to void * or to
unambiguous base classes.
Mixing code compiled with -frtti with that compiled with
-fno-rtti may not work. For example, programs may
fail to link if a class compiled with -fno-rtti is used as a base
for a class compiled with -frtti.
-fsized-deallocation void operator delete (void *, std::size_t) noexcept;
void operator delete[] (void *, std::size_t) noexcept;
as introduced in C++14. This is useful for user-defined replacement
deallocation functions that, for example, use the size of the object
to make deallocation faster. Enabled by default under
-std=c++14 and above. The flag -Wsized-deallocation
warns about places that might want to add a definition.
-fstrict-enums-fstrong-eval-order-ftemplate-backtrace-limit=n-ftemplate-depth=n-fno-threadsafe-statics-fuse-cxa-atexit__cxa_atexit function rather than the atexit function.
This option is required for fully standards-compliant handling of static
destructors, but only works if your C library supports
__cxa_atexit.
-fno-use-cxa-get-exception-ptr__cxa_get_exception_ptr runtime routine. This
causes std::uncaught_exception to be incorrect, but is necessary
if the runtime routine is not available.
-fvisibility-inlines-hiddenThe effect of this is that GCC may, effectively, mark inline methods with
__attribute__ ((visibility ("hidden"))) so that they do not
appear in the export table of a DSO and do not require a PLT indirection
when used within the DSO. Enabling this option can have a dramatic effect
on load and link times of a DSO as it massively reduces the size of the
dynamic export table when the library makes heavy use of templates.
The behavior of this switch is not quite the same as marking the methods as hidden directly, because it does not affect static variables local to the function or cause the compiler to deduce that the function is defined in only one shared object.
You may mark a method as having a visibility explicitly to negate the effect of the switch for that method. For example, if you do want to compare pointers to a particular inline method, you might mark it as having default visibility. Marking the enclosing class with explicit visibility has no effect.
Explicitly instantiated inline methods are unaffected by this option
as their linkage might otherwise cross a shared library boundary.
See Template Instantiation.
-fvisibility-ms-compatThe flag makes these changes to GCC's linkage model:
hidden, like
-fvisibility=hidden.
In new code it is better to use -fvisibility=hidden and export those classes that are intended to be externally visible. Unfortunately it is possible for code to rely, perhaps accidentally, on the Visual Studio behavior.
Among the consequences of these changes are that static data members
of the same type with the same name but defined in different shared
objects are different, so changing one does not change the other;
and that pointers to function members defined in different shared
objects may not compare equal. When this flag is given, it is a
violation of the ODR to define types with the same name differently.
-fno-weak-fext-numeric-literals (C++ and Objective-C++ only)-nostdinc++-flang-info-include-translate-flang-info-include-translate-not-flang-info-include-translate=header"user" or <system> it will be resolved to a
specific user or system header using the include path.
-flang-info-module-cmi-flang-info-module-cmi=module<> or "").
-stdlib=libstdc++,libc++-lstdc++ or -lc++ respectively,
when a C++ runtime is required for linking.
In addition, these warning options have meanings only for C++ programs:
-Wabi-tag (C++ and Objective-C++ only)-Wcomma-subscript (C++ and Objective-C++ only)( ) is not deprecated. Example:
void f(int *a, int b, int c) {
a[b,c]; // deprecated in C++20, invalid in C++23
a[(b,c)]; // OK
}
In C++23 it is valid to have comma separated expressions in a subscript when an overloaded subscript operator is found and supports the right number and types of arguments. G++ will accept the formerly valid syntax for code that is not valid in C++23 but used to be valid but deprecated in C++20 with a pedantic warning that can be disabled with -Wno-comma-subscript.
Enabled by default with -std=c++20 unless -Wno-deprecated,
and with -std=c++23 regardless of -Wno-deprecated.
-Wctad-maybe-unsupported (C++ and Objective-C++ only) struct allow_ctad_t; // any name works
template <typename T> struct S {
S(T) { }
};
S(allow_ctad_t) -> S<void>; // guide with incomplete parameter type will never be considered
-Wctor-dtor-privacy (C++ and Objective-C++ only)-Wdelete-non-virtual-dtor (C++ and Objective-C++ only)delete is used to destroy an instance of a class that
has virtual functions and non-virtual destructor. It is unsafe to delete
an instance of a derived class through a pointer to a base class if the
base class does not have a virtual destructor. This warning is enabled
by -Wall.
-Wdeprecated-copy (C++ and Objective-C++ only)-Wno-deprecated-enum-enum-conversion (C++ and Objective-C++ only) enum E1 { e };
enum E2 { f };
int k = f - e;
-Wdeprecated-enum-enum-conversion is enabled by default with
-std=c++20. In pre-C++20 dialects, this warning can be enabled
by -Wenum-conversion.
-Wno-deprecated-enum-float-conversion (C++ and Objective-C++ only) enum E1 { e };
enum E2 { f };
bool b = e <= 3.7;
-Wdeprecated-enum-float-conversion is enabled by default with
-std=c++20. In pre-C++20 dialects, this warning can be enabled
by -Wenum-conversion.
-Wno-init-list-lifetime (C++ and Objective-C++ only)std::initializer_list that are likely
to result in dangling pointers. Since the underlying array for an
initializer_list is handled like a normal C++ temporary object,
it is easy to inadvertently keep a pointer to the array past the end
of the array's lifetime. For example:
initializer_list, or a local
initializer_list variable, the array's lifetime ends at the end
of the return statement, so the value returned has a dangling pointer.
initializer_list, the array only
lives until the end of the enclosing full-expression, so the
initializer_list in the heap has a dangling pointer.
initializer_list variable is assigned from a
brace-enclosed initializer list, the temporary array created for the
right side of the assignment only lives until the end of the
full-expression, so at the next statement the initializer_list
variable has a dangling pointer.
// li's initial underlying array lives as long as li
std::initializer_list<int> li = { 1,2,3 };
// assignment changes li to point to a temporary array
li = { 4, 5 };
// now the temporary is gone and li has a dangling pointer
int i = li.begin()[0] // undefined behavior
begin pointer from the
initializer_list argument, this doesn't extend the lifetime of
the array, so if a class variable is constructed from a temporary
initializer_list, the pointer is left dangling by the end of
the variable declaration statement.
-Winvalid-imported-macros-Wno-literal-suffix (C++ and Objective-C++ only)<inttypes.h>. For example:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdio.h>
int main() {
int64_t i64 = 123;
printf("My int64: %" PRId64"\n", i64);
}
In this case, PRId64 is treated as a separate preprocessing token.
This option also controls warnings when a user-defined literal operator is declared with a literal suffix identifier that doesn't begin with an underscore. Literal suffix identifiers that don't begin with an underscore are reserved for future standardization.
These warnings are enabled by default.
-Wno-narrowing (C++ and Objective-C++ only)With -Wnarrowing in C++98, warn when a narrowing conversion prohibited by C++11 occurs within `{ }', e.g.
int i = { 2.2 }; // error: narrowing from double to int
This flag is included in -Wall and -Wc++11-compat.
-Wnoexcept (C++ and Objective-C++ only)throw() or noexcept) but is known by
the compiler to never throw an exception.
-Wnoexcept-type (C++ and Objective-C++ only)noexcept part of a function
type changes the mangled name of a symbol relative to C++14. Enabled
by -Wabi and -Wc++17-compat.
As an example:
template <class T> void f(T t) { t(); };
void g() noexcept;
void h() { f(g); }
In C++14, f calls f<void(*)()>, but in
C++17 it calls f<void(*)()noexcept>.
-Wclass-memaccess (C++ and Objective-C++ only)memset or memcpy is an object of class type, and when writing
into such an object might bypass the class non-trivial or deleted constructor
or copy assignment, violate const-correctness or encapsulation, or corrupt
virtual table pointers. Modifying the representation of such objects may
violate invariants maintained by member functions of the class. For example,
the call to memset below is undefined because it modifies a non-trivial
class object and is, therefore, diagnosed. The safe way to either initialize
or clear the storage of objects of such types is by using the appropriate
constructor or assignment operator, if one is available.
std::string str = "abc";
memset (&str, 0, sizeof str);
The -Wclass-memaccess option is enabled by -Wall.
Explicitly casting the pointer to the class object to void * or
to a type that can be safely accessed by the raw memory function suppresses
the warning.
-Wnon-virtual-dtor (C++ and Objective-C++ only)-Wregister (C++ and Objective-C++ only)register storage class specifier, except
when it is part of the GNU Explicit Register Variables extension.
The use of the register keyword as storage class specifier has
been deprecated in C++11 and removed in C++17.
Enabled by default with -std=c++17.
-Wreorder (C++ and Objective-C++ only) struct A {
int i;
int j;
A(): j (0), i (1) { }
};
The compiler rearranges the member initializers for i
and j to match the declaration order of the members, emitting
a warning to that effect. This warning is enabled by -Wall.
-Wno-pessimizing-move (C++ and Objective-C++ only)std::move prevents copy
elision. A typical scenario when copy elision can occur is when returning in
a function with a class return type, when the expression being returned is the
name of a non-volatile automatic object, and is not a function parameter, and
has the same type as the function return type.
struct T {
...
};
T fn()
{
T t;
...
return std::move (t);
}
But in this example, the std::move call prevents copy elision.
This warning is enabled by -Wall.
-Wno-redundant-move (C++ and Objective-C++ only)std::move; that is, when
a move operation would have been performed even without the std::move
call. This happens because the compiler is forced to treat the object as if
it were an rvalue in certain situations such as returning a local variable,
where copy elision isn't applicable. Consider:
struct T {
...
};
T fn(T t)
{
...
return std::move (t);
}
Here, the std::move call is redundant. Because G++ implements Core
Issue 1579, another example is:
struct T { // convertible to U
...
};
struct U {
...
};
U fn()
{
T t;
...
return std::move (t);
}
In this example, copy elision isn't applicable because the type of the expression being returned and the function return type differ, yet G++ treats the return value as if it were designated by an rvalue.
This warning is enabled by -Wextra.
-Wrange-loop-construct (C++ and Objective-C++ only) struct S { char arr[128]; };
void fn () {
S arr[5];
for (const auto x : arr) { ... }
}
It does not warn when the type being copied is a trivially-copyable type whose size is less than 64 bytes.
This warning also warns when a loop variable in a range-based for-loop is initialized with a value of a different type resulting in a copy. For example:
void fn() {
int arr[10];
for (const double &x : arr) { ... }
}
In the example above, in every iteration of the loop a temporary value of
type double is created and destroyed, to which the reference
const double & is bound.
This warning is enabled by -Wall.
-Wredundant-tags (C++ and Objective-C++ only) struct foo;
struct foo *p; // warn that keyword struct can be eliminated
On the other hand, in this example there is no warning:
struct foo;
void foo (); // "hides" struct foo
void bar (struct foo&); // no warning, keyword struct is necessary
-Wno-subobject-linkage (C++ and Objective-C++ only)-Weffc++ (C++ and Objective-C++ only)operator= return a reference to *this.
&&, ||, or ,.
This option also enables -Wnon-virtual-dtor, which is also one of the effective C++ recommendations. However, the check is extended to warn about the lack of virtual destructor in accessible non-polymorphic bases classes too.
When selecting this option, be aware that the standard library
headers do not obey all of these guidelines; use `grep -v'
to filter out those warnings.
-Wno-exceptions (C++ and Objective-C++ only)-Wstrict-null-sentinel (C++ and Objective-C++ only)NULL as sentinel. When
compiling only with GCC this is a valid sentinel, as NULL is defined
to __null. Although it is a null pointer constant rather than a
null pointer, it is guaranteed to be of the same size as a pointer.
But this use is not portable across different compilers.
-Wno-non-template-friend (C++ and Objective-C++ only)-Wold-style-cast (C++ and Objective-C++ only)dynamic_cast,
static_cast, reinterpret_cast, and const_cast) are
less vulnerable to unintended effects and much easier to search for.
-Woverloaded-virtual (C++ and Objective-C++ only) struct A {
virtual void f();
};
struct B: public A {
void f(int);
};
the A class version of f is hidden in B, and code
like:
B* b;
b->f();
fails to compile.
-Wno-pmf-conversions (C++ and Objective-C++ only)-Wsign-promo (C++ and Objective-C++ only)-Wtemplates (C++ and Objective-C++ only)-Wmismatched-new-delete (C++ and Objective-C++ only)operator new or operator
delete and the corresponding call to the allocation or deallocation function.
This includes invocations of C++ operator delete with pointers
returned from either mismatched forms of operator new, or from other
functions that allocate objects for which the operator delete isn't
a suitable deallocator, as well as calls to other deallocation functions
with pointers returned from operator new for which the deallocation
function isn't suitable.
For example, the delete expression in the function below is diagnosed
because it doesn't match the array form of the new expression
the pointer argument was returned from. Similarly, the call to free
is also diagnosed.
void f ()
{
int *a = new int[n];
delete a; // warning: mismatch in array forms of expressions
char *p = new char[n];
free (p); // warning: mismatch between new and free
}
The related option -Wmismatched-dealloc diagnoses mismatches
involving allocation and deallocation functions other than operator
new and operator delete.
-Wmismatched-new-delete is included in -Wall.
-Wmismatched-tags (C++ and Objective-C++ only)For example, the declaration of struct Object in the argument list
of draw triggers the warning. To avoid it, either remove the redundant
class-key struct or replace it with class to match its definition.
class Object {
public:
virtual ~Object () = 0;
};
void draw (struct Object*);
It is not wrong to declare a class with the class-key struct as
the example above shows. The -Wmismatched-tags option is intended
to help achieve a consistent style of class declarations. In code that is
intended to be portable to Windows-based compilers the warning helps prevent
unresolved references due to the difference in the mangling of symbols
declared with different class-keys. The option can be used either on its
own or in conjunction with -Wredundant-tags.
-Wmultiple-inheritance (C++ and Objective-C++ only)-Wvirtual-inheritance-Wno-virtual-move-assign-Wnamespaces-Wno-terminate (C++ and Objective-C++ only)terminate.
-Wno-vexing-parse (C++ and Objective-C++ only) void f(double a) {
int i(); // extern int i (void);
int n(int(a)); // extern int n (int);
}
Another example:
struct S { S(int); };
void f(double a) {
S x(int(a)); // extern struct S x (int);
S y(int()); // extern struct S y (int (*) (void));
S z(); // extern struct S z (void);
}
The warning will suggest options how to deal with such an ambiguity; e.g., it can suggest removing the parentheses or using braces instead.
This warning is enabled by default.
-Wno-class-conversion (C++ and Objective-C++ only)-Wvolatile (C++ and Objective-C++ only)volatile qualifier. This includes
postfix and prefix ++ and -- expressions of
volatile-qualified types, using simple assignments where the left
operand is a volatile-qualified non-class type for their value,
compound assignments where the left operand is a volatile-qualified
non-class type, volatile-qualified function return type,
volatile-qualified parameter type, and structured bindings of a
volatile-qualified type. This usage was deprecated in C++20.
Enabled by default with -std=c++20.
-Wzero-as-null-pointer-constant (C++ and Objective-C++ only)nullptr in C++11.
-Waligned-newalignof(std::max_align_t) but uses an allocation
function without an explicit alignment parameter. This option is
enabled by -Wall.
Normally this only warns about global allocation functions, but
-Waligned-new=all also warns about class member allocation
functions.
-Wno-placement-new-Wplacement-new=n char buf [64];
new (buf) int[64];
This warning is enabled by default.
-Wplacement-new=1new expression is not diagnosed at this level even
though it has undefined behavior according to the C++ standard because
it writes past the end of the one-element array.
struct S { int n, a[1]; };
S *s = (S *)malloc (sizeof *s + 31 * sizeof s->a[0]);
new (s->a)int [32]();
-Wplacement-new=2 struct S { int n, a[]; };
S *s = (S *)malloc (sizeof *s + 32 * sizeof s->a[0]);
new (s->a)int [32]();
-Wcatch-value-Wcatch-value=n (C++ and Objective-C++ only)-Wconditionally-supported (C++ and Objective-C++ only)-Wno-delete-incomplete (C++ and Objective-C++ only)-Wextra-semi (C++, Objective-C++ only)-Wno-inaccessible-base (C++, Objective-C++ only) struct A { int a; };
struct B : A { };
struct C : B, A { };
-Wno-inherited-variadic-ctor-Wno-invalid-offsetof (C++ and Objective-C++ only)offsetof macro to a non-POD
type. According to the 2014 ISO C++ standard, applying offsetof
to a non-standard-layout type is undefined. In existing C++ implementations,
however, offsetof typically gives meaningful results.
This flag is for users who are aware that they are
writing nonportable code and who have deliberately chosen to ignore the
warning about it.
The restrictions on offsetof may be relaxed in a future version
of the C++ standard.
-Wsized-deallocation (C++ and Objective-C++ only) void operator delete (void *) noexcept;
void operator delete[] (void *) noexcept;
without a definition of the corresponding sized deallocation function
void operator delete (void *, std::size_t) noexcept;
void operator delete[] (void *, std::size_t) noexcept;
or vice versa. Enabled by -Wextra along with
-fsized-deallocation.
-Wsuggest-final-typesfinal specifier,
or, if possible,
declared in an anonymous namespace. This allows GCC to more aggressively
devirtualize the polymorphic calls. This warning is more effective with
link-time optimization,
where the information about the class hierarchy graph is
more complete.
-Wsuggest-final-methodsfinal specifier,
or, if possible, its type were
declared in an anonymous namespace or with the final specifier.
This warning is
more effective with link-time optimization, where the information about the
class hierarchy graph is more complete. It is recommended to first consider
suggestions of -Wsuggest-final-types and then rebuild with new
annotations.
-Wsuggest-overrideoverride keyword.
-Wuse-after-free-Wuse-after-free=n-Wuse-after-free=1realloc, regardless of whether or not the call resulted
in an actual reallocatio of memory. This includes double-free
calls as well as uses in arithmetic and relational expressions. Although
undefined, uses of indeterminate pointers in equality (or inequality)
expressions are not diagnosed at this level.
-Wuse-after-free=2free in the following
function is diagnosed at this level:
struct A { int refcount; void *data; };
void release (struct A *p)
{
int refcount = --p->refcount;
free (p);
if (refcount == 0)
free (p->data); // warning: p may be used after free
}
-Wuse-after-free=3realloc as
an attempt to determine whether the call resulted in relocating the object
to a different address. They are diagnosed at a separate level to aid
legacy code gradually transition to safe alternatives. For example,
the equality test in the function below is diagnosed at this level:
void adjust_pointers (int**, int);
void grow (int **p, int n)
{
int **q = (int**)realloc (p, n *= 2);
if (q == p)
return;
adjust_pointers ((int**)q, n);
}
To avoid the warning at this level, store offsets into allocated memory instead of pointers. This approach obviates needing to adjust the stored pointers after reallocation.
-Wuse-after-free=2 is included in -Wall.
-Wuseless-cast (C++ and Objective-C++ only)-Wno-conversion-null (C++ and Objective-C++ only)NULL and non-pointer
types. -Wconversion-null is enabled by default.