mercredi 22 juin 2022

converting to ‘A’ from initializer list would use explicit constructor ‘A::A(int)’

I am trying to migrate an old c++03 codebase to c++11. But I fails to understand what gcc is warning me about in the following case:

% g++ -std=c++03 t.cxx
% g++ -std=c++11 t.cxx
t.cxx: In function ‘int main()’:
t.cxx:8:21: warning: converting to ‘A’ from initializer list would use explicit constructor ‘A::A(int)’
    8 | int main() { B b = {}; }
      |                     ^
t.cxx:8:21: note: in C++11 and above a default constructor can be explicit

where

struct A {
  explicit A(int i = 42) {}
};
struct B {
  A a;
};

int main() {
  B b = {};
  return 0;
}

All I am trying to do is a basic zero initialization here. It seems to be legal for c++03 but I fail to understand how to express the equivalent in c++11.


For reference, I am using:

% g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

Aucun commentaire:

Enregistrer un commentaire