gcc warning" 'will be initialized after'

G++Suppress Warnings

G++ Problem Overview


I am getting a lot of these warnings from 3rd party code that I cannot modify. Is there a way to disable this warning or at least disable it for certain areas (like #pragma push/pop in VC++)?

Example:

list.h:1122: warning: `list<LogOutput*, allocator<LogOutput*> >::node_alloc_' will be initialized after 
list.h:1117: warning:   `allocator<LogOutput*> list<LogOutput*, allocator<LogOutput*> >::alloc_'

G++ Solutions


Solution 1 - G++

Make sure the members appear in the initializer list in the same order as they appear in the class

Class C {
   int a;
   int b;
   C():b(1),a(2){} //warning, should be C():a(2),b(1)
}

or you can turn -Wno-reorder

Solution 2 - G++

You can disable it with -Wno-reorder.

Solution 3 - G++

For those using QT having this error, add this to .pro file

QMAKE_CXXFLAGS_WARN_ON += -Wno-reorder

Solution 4 - G++

use -Wno-reorder (man gcc is your friend :) )

Solution 5 - G++

If you're seeing errors from library headers and you're using GCC, then you can disable warnings by including the headers using -isystem instead of -I.

Similar features exist in clang.

If you're using CMake, you can specify SYSTEM for include_directories.

Solution 6 - G++

Class C {
   int a;
   int b;
   C():b(1),a(2){} //warning, should be C():a(2),b(1)
}

the order is important because if a is initialized before b , and a is depend on b. undefined behavior will appear.

Solution 7 - G++

The order of initialization doesn’t matter. All fields are initialized in the order of their definition in their class/struct. But if the order in initialization list is different gcc/g++ generate this warning. Only change the initialization order to avoid this warning. But you can't define field using in initialization before its construct. It will be a runtime error. So you change the order of definition. Be careful and keep attention!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLK__View Question on Stackoverflow
Solution 1 - G++urayView Answer on Stackoverflow
Solution 2 - G++Lukáš LalinskýView Answer on Stackoverflow
Solution 3 - G++user1175197View Answer on Stackoverflow
Solution 4 - G++LaszloGView Answer on Stackoverflow
Solution 5 - G++Drew NoakesView Answer on Stackoverflow
Solution 6 - G++SamuelView Answer on Stackoverflow
Solution 7 - G++AnatolyView Answer on Stackoverflow