Building G4.11 on Windows with VS2019 v16.11.8

Dear Geant4 team,
I am trying to update my previous installation of GEANT4 (G4.11preview) to the released one.
On Ubuntu20.04, everything goes well.
However, on windows, I have some problems with VS2019 v16.11.8.
Indeed, the compiler gives me multiple errors C2666 for some .icc files, in particular for G4String.icc.
For the example of G4String.icc, compiler seems to not accept the overloading of the “starts_with” function as it is pointing line 236 which corresponds to the second definition of:

  inline bool starts_with(const G4String& str, G4String::value_type ss)
  {
    return !str.empty() && (str[0] == ss);
  }

The C2666 error is the error identified as ‘number overloads have similar conversions’ according to Compiler Error C2666 | Microsoft Docs.

I do not know if you are aware of some parameters I should change in my compiler, or if I should downgrade to a previous version.

Thanks for your help
Best regards

What versions for MSVC etc are printed when you run CMake to configure Geant4? It’s usually the first few lines like:

-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.25.28612.0
-- The CXX compiler identification is MSVC 19.25.28612.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe - works

Please find below the result of cmake command

Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.22000.
The C compiler identification is MSVC 19.29.30138.0
The CXX compiler identification is MSVC 19.29.30138.0
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working C compiler: D:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x86/cl.exe - skipped
Detecting C compile features
Detecting C compile features - done
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check for working CXX compiler: D:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x86/cl.exe - skipped
Detecting CXX compile features
Detecting CXX compile features - done
Checking C++ feature CXXSTDLIB_FILESYSTEM_NATIVE - Success
Reusing dataset G4NDL (4.6)
Reusing dataset G4EMLOW (8.0)
Reusing dataset PhotonEvaporation (5.7)
Reusing dataset RadioactiveDecay (5.6)
Reusing dataset G4PARTICLEXS (4.0)
Reusing dataset G4PII (1.3)
Reusing dataset RealSurface (2.2)
Reusing dataset G4SAIDDATA (2.0)
Reusing dataset G4ABLA (3.1)
Reusing dataset G4INCL (1.0)
Reusing dataset G4ENSDFSTATE (2.3)
Reusing dataset G4TENDL (1.4)
The following Geant4 features are enabled:
CMAKE_CXX_STANDARD: Compiling against C++ Standard '17'
GEANT4_BUILD_MULTITHREADED: Build multithread enabled libraries
GEANT4_USE_INVENTOR_QT: Build OpenInventor Qt Driver
GEANT4_USE_QT: Build Geant4 with Qt support
GEANT4_USE_OPENGL_WIN32: Build OpenGL driver with Win32 support
GEANT4_USE_QT3D: Build Geant4 Qt3D driver
Configuring done
Generating done

Thanks! I think our Windows testing node is probably behind the version that introduced the update mentioned on Compiler Error C2666 | Microsoft Docs

We’ll get that updated and a fix prepared (could you also post the full compiler error message here please?). In the meantime, it’s probably sufficient to remove all bar the declarations/definitions of:

  inline bool starts_with(const G4String& str, std::string_view ss);

  inline bool ends_with(const G4String& str, std::string_view ss);

in the G4String.hh/.icc files in your source tree.

Sure. Please find below a copy of the error message. Sorry that it is in french

I want to mention that it is not only linked to G4String.icc.
I found the following files with issues but did not finish the compilation yet so it might not be the full list. I will then update my post in the next hours (UPDATED NOW):

-G4CascadeParamMessenger.cc line 139
-G4InteractorMessenger.cc line 220
-G4MicroElectMaterialStructure.cc lines 105 and 136
-G4SDManager.cc lines 74 and 128 and 137
-G4UIArrayString.cc line 71
-G4UIbridge.cc line 38
-G4UIcommand.cc lines 168, 171, 218, 233, 1596, 1606, 1613
-G4UICommandTree.cc lines 519, 532
-G4UIcontrolMessenger.cc lines 595, 718
-G4UIExecutive.cc line 222
-G4UImanager.cc lines 380, 740
-G4UIparameter.cc lines 1060, 1070, 1078
-G4UIQt.cc lines 2395, 2398
-G4UIterminal.cc lines 252, 263
-G4VBasicShell.cc lines 50, 131, 243, 264, 275
-G4VisCommandsScene.cc lines 633
-G4VisCommandsSceneAdd.cc lines 437 - 439, 679 - 681, 1659 - 1665, 1978 - 1980, 2268, 2269
-G4VisManager.cc lines 2414 - 2420
-G4VSensitiveDetector.cc line 50
-G4VUIshelll.cc lines 92, 96, 102, 176

I think there should be an option in MSVC to neglect those errors since it is working well with previous versions. It will be quite complex to modify all the files. Also this C2666 does not make sense to me since the compiler should understand the overload correctly.

I have checked random classes in my list above and the errors in those classes come from the use of G4String so, everything seems to be linked to G4String.icc

I will summarize here the error and how I fixed it.

When compiling GEANT4.11.0 on windows using VS2019 v16.11.8 (in fact it will happen with all versions above 16.1), the compiler provides multiple errors C2666 indicating for operator[] from G4String: number overloads have similar conversions.

This comes from a change introduced recently in MSVC, according to C++ conformance improvements in Visual Studio 2019 | Microsoft Docs

The way I fixed the problem is detailed below. Please note that I also tried to compile the code with my modifications on Linux and to execute it on a simple example with success.

In the different files I mentioned in my previous message above, the problem comes from the use of
G4String::operator[] instead of G4String::at()

At multiple locations in the code, one can find (for example)

if (!(jslash == indx && bpath[indx] == '/'))
// Whereas the correct use should be
if (!(jslash == indx && bpath.at(indx) == '/'))

Please find below a patch that I will also submit on Bugzilla.
G4_11_patch_String.txt (21.8 KB)

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