3

Okay, I know this is similar to this Why is the compiler throwing this warning: "missing initializer"? Isn't the structure initialized?, but surely GCC isn't stupid enough to think I meant to initialize something which has NO initialization?.

//main.cpp
struct IsNamed{
};

template<typename T>
struct Test{
    int foo;
};
struct Test2 : public Test<double>, public IsNamed{

};
int main(){
    Test2 x;

    Test2 y = Test2{Test<double>{}};
    return 0;
}

Here's the output:

main2.cpp: In function 'int main()':
main2.cpp:18:35: warning: missing initializer for member 'Test2::<anonymous>' [-Wmissing-field-initializers]
   18 |     Test2 y = Test2{Test<double>{}};
      |                                   ^
main2.cpp:16:11: warning: unused variable 'x' [-Wunused-variable]
   16 |     Test2 x;
      |           ^
main2.cpp:18:11: warning: variable 'y' set but not used [-Wunused-but-set-variable]
   18 |     Test2 y = Test2{Test<double>{}};
      |    

The only warning I'm confused about is this " missing initializer for member 'Test2:: " warning. It makes no sense. There's only one possible value I would need, and I provide it. Just to prove that it is indeed the inclusion of the empty class in the inheritance chain that is causing this, here's the output if I remove it:

main2.cpp: In function 'int main()':
main2.cpp:16:11: warning: unused variable 'x' [-Wunused-variable]
   16 |     Test2 x;
      |           ^
main2.cpp:18:11: warning: variable 'y' set but not used [-Wunused-but-set-variable]
   18 |     Test2 y = Test2{Test<double>{}};
      |  

People say just ignore it, but it's not feasible for my project. I've got lots of classes with this error that follow the same tag struct pattern. It's not reasonable to selectively disable this warning (I also find it useful in other circumstances).

How do I get the compiler to stop complaining about this? I should also mention that Test2{Test<double>{},{}}; would not be an appropriate solution, as I have template code in use where some of the classes do not have this problem, and some do, which means this becomes an error in those circumstances.

EDIT:

Note here is the example CMake used, which included the compiler options:

cmake_minimum_required(VERSION 3.13)
project(test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

add_executable(test main.cpp)
target_include_directories(test
        PRIVATE
        ./
        )
target_compile_options(test PRIVATE
        $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>>:
        -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -g -ggdb -O1>)

I'm also using mingw64-10.2

Krupip
  • 4,404
  • 2
  • 32
  • 54
  • What would happen if you initialized as `Test2 y = Test2{Test{}, IsNamed{}}`? – IWonderWhatThisAPIDoes Jan 26 '21 at 07:08
  • Could you add the version of GCC you're using and the exact compiler flags. I'm not seeing this on GCC10.2 with `-Wall`. – cigien Jan 26 '21 at 07:10
  • @cigien `-Wextra` does the trick for me. – n314159 Jan 26 '21 at 13:14
  • The only way to get around this warning on gcc is to define contructors, which of course causes `Test2` to not be an aggregate anymore. You could also file a bug with gcc (clang does not warn for this) but this won't be a fast fix, if it is even considered a bug and fixed at all. – n314159 Jan 26 '21 at 13:17

1 Answers1

0

There's only one possible value I would need, and I provide it

The warning isn't about providing values that you "need". After all, the compiler cannot in general know which values you think are needed.

The warning is about providing all initialisers. And there is no initialiser for the other base.

How do I get the compiler to stop complaining about this?

Provide the missing initialiser:

Test2 y = Test2{Test<double>{}, {}};

Or just disable the warning. Or use another compiler; Clang appears to not warn in your case for example.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Just because the compiler does not give a warning it does not mean that the code is OK. Clang has been wrong before... – U. W. Jan 26 '21 at 08:28
  • @U.W. So has GCC. Just because the compiler gives a warning, doesn't necessarily mean that there is anything wrong with the program. – eerorika Jan 26 '21 at 08:31
  • True enough! That makes writing software in C++ sooo much fun!! :-) – U. W. Jan 26 '21 at 08:36