Compiler warning from class with G4String data member

Geant4 Version: 10.7.p04
Operating System: MacOS 10.15.7
Compiler/Version: Apple Clang 12.0.0
CMake Version: 3.15.2

I am getting a weird compiler warning, which I don’t think should happen at all. I have a class with a G4String data member, in which I’ve followed the Rule of Five to declare the various copy constructors, thus:

  // Use default copy/move semantics
  G4LatticeReader(const G4LatticeReader&) = default;
  G4LatticeReader(G4LatticeReader&&) = default;
  G4LatticeReader& operator=(const G4LatticeReader&) = default;
  G4LatticeReader& operator=(G4LatticeReader&&) = default;

I’m getting a compiler warning for the two assignment operators, which claims (incorrectly!) that G4String doesn’t have the corresponding operators:

./include/G4LatticeReader.hh:46:20: warning: explicitly defaulted copy
      assignment operator is implicitly deleted [-Wdefaulted-function-deleted]
  G4LatticeReader& operator=(const G4LatticeReader&) = default;
                   ^
./include/G4LatticeReader.hh:97:18: note: copy assignment operator of
      'G4LatticeReader' is implicitly deleted because field 'fDataDir' has no
      copy assignment operator
  const G4String fDataDir;      // Directory path ($G4LATTICEDATA)
                 ^
./include/G4LatticeReader.hh:47:20: warning: explicitly defaulted move
      assignment operator is implicitly deleted [-Wdefaulted-function-deleted]
  G4LatticeReader& operator=(G4LatticeReader&&) = default;
                   ^
./include/G4LatticeReader.hh:97:18: note: move assignment operator of
      'G4LatticeReader' is implicitly deleted because field 'fDataDir' has no
      move assignment operator
  const G4String fDataDir;      // Directory path ($G4LATTICEDATA)
                 ^
2 warnings generated.

But that is trivially incorrect. From G4String.hh:

  inline G4String(G4String&&) = default;

and

  inline G4String& operator=(G4String&&) = default;

Has anyone else seen this kind of compiler warning? Or do I just have some incorrect compiler option going on?

Hi Mike, this is eventually due to the fact your class G4LatticeReader has a const G4String as data-member, so the compiler implicitly deletes your copy assignment/move operators, being fDataDir a non-copyable object. So, unless G4LatticeReader has other copyable data members to justify an explicit implementation of copy assignment/move operators, just declare them as delete, or… explicitly implement them anyhow, assuming the path fDataDir won’t change.

Oh! Now I understand, Gabriele. The fDataDir member has no methods to change it – it’s set in the constructor. After that change, I got the same warnings because of a const G4double data member.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.