Problem 1803

Summary: Missing definition of remove_reference in standard headers
Product: Geant4 Reporter: Nicola Mori <mori>
Component: intercomsAssignee: Ivana.Hrivnacova
Status: RESOLVED FIXED    
Severity: major CC: asai, mori
Priority: P5    
Version: 10.2   
Hardware: All   
OS: Linux   
Attachments: Explicitly remove constness with remove_const

Description Nicola Mori 2015-12-07 23:10:23 CET
The recent 10.02 release ships a patched version of G4AnyMethod.hh which makes use of the definitions of remove_reference given in the standard header type_traits when compiling with support for c++11. This header in my system (gcc 5.2) defines these three prototypes:

  template<typename _Tp>
    struct remove_reference
    { typedef _Tp   type; };

  template<typename _Tp>
    struct remove_reference<_Tp&>
    { typedef _Tp   type; };

  template<typename _Tp>
    struct remove_reference<_Tp&&>
    { typedef _Tp   type; };

The old pre-c++11 implementation in G4AnyMethod.hh defines these:

  template<typename T> struct remove_reference {typedef T type;};
  template<typename T> struct remove_reference<T&> {typedef T type;};
  template<typename T> struct remove_reference<const T&> {typedef T type;};

The third definition is different, and in particular it also removes the const qualifier. The net effect is that when compiling with c++11 support it is not possible to call G4GenericMessenger::SetMethod using as second argument a method which takes a const argument, since at line 185 of G4AnyMethod.hh the rhs variable a0 is const and the compiler gives an error. This does not happen when compiling with the c++98 standard.
Comment 1 Nicola Mori 2015-12-09 11:41:21 CET
Created attachment 368 [details]
Explicitly remove constness with remove_const

A very simple fix is to explicitly remove constness by means of remove_const. This is a C++11 feature but it's very simple to implement also for C++98 like what was done for remove_reference. I attach a patch which does exactly this. It works on my system when using C++11, I didn't test it with C++98 since I didn't find a way to force C++98 (CMake only accepts 11 or 14 as possible options). Feel free to use it as you like.
Comment 2 Ivana.Hrivnacova 2017-08-18 14:46:48 CEST
The suggested fix was applied in Geant4 development version and will be available in the next release, 10.4. Closing.