‘string_view’ is not a member of ‘std’ error. c++ version error?

_ROOT Version:6.24
_Platform: WSL (Ubuntu 20.04)
_Compiler: gcc 9.3.0, cmake 3.16.3

Dear all,

Hi. I’m trying to compile geant4 example written with ROOT. But I encountered following errors. It looks like an error of c++ version, but when I force the c++ version into c++17, using ‘CMAKE_CXX_FLAGS=-std=c++17’ then the compiling is totally crashed with 5k error lines. Should I install the downgraded version of root?

Scanning dependencies of target sim
[  8%] Building CXX object CMakeFiles/sim.dir/sim.cc.o
[ 16%] Building CXX object CMakeFiles/sim.dir/src/ActionInitialization.cc.o
In file included from /home/jwbae/root/include/TString.h:29,
                 from /home/jwbae/root/include/TNamed.h:26,
                 from /home/jwbae/root/include/TDirectory.h:24,
                 from /home/jwbae/root/include/TDirectoryFile.h:25,
                 from /home/jwbae/root/include/TFile.h:28,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/include/RunAction.hh:39,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/src/ActionInitialization.cc:33:
/home/jwbae/root/include/ROOT/RStringView.hxx:32:37: error: ‘experimental’ in namespace ‘std’ does not name a type
   32 |    using basic_string_view = ::std::experimental::basic_string_view<_CharT,_Traits>;
      |                                     ^~~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:35:12: error: ‘basic_string_view’ does not name a type
   35 |    typedef basic_string_view<char> string_view;
      |            ^~~~~~~~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:36:12: error: ‘basic_string_view’ does not name a type
   36 |    typedef basic_string_view<char16_t> u16string_view;
      |            ^~~~~~~~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:37:12: error: ‘basic_string_view’ does not name a type
   37 |    typedef basic_string_view<char32_t> u32string_view;
      |            ^~~~~~~~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:38:12: error: ‘basic_string_view’ does not name a type
   38 |    typedef basic_string_view<wchar_t> wstring_view;
      |            ^~~~~~~~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:48:4: warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
   48 |    inline double stod(std::string_view str, size_t *pos)
      |    ^~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:48:28: error: ‘string_view’ is not a member of ‘std’
   48 |    inline double stod(std::string_view str, size_t *pos)
      |                            ^~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:48:28: note: ‘std::string_view’ is only available from C++17 onwards
/home/jwbae/root/include/ROOT/RStringView.hxx:48:52: error: expected primary-expression before ‘*’ token
   48 |    inline double stod(std::string_view str, size_t *pos)
      |                                                    ^
/home/jwbae/root/include/ROOT/RStringView.hxx:48:53: error: ‘pos’ was not declared in this scope; did you mean ‘pow’?
   48 |    inline double stod(std::string_view str, size_t *pos)
      |                                                     ^~~
      |                                                     pow
/home/jwbae/root/include/ROOT/RStringView.hxx:48:56: error: expression list treated as compound expression in initializer [-fpermissive]
   48 |    inline double stod(std::string_view str, size_t *pos)
      |                                                        ^
/home/jwbae/root/include/ROOT/RStringView.hxx:65:56: error: ‘std::string_view’ has not been declared
   65 | inline std::string &operator+=(std::string &left, std::string_view right)
      |                                                        ^~~~~~~~~~~
/home/jwbae/root/include/ROOT/RStringView.hxx: In function ‘std::string& std::__ROOT::operator+=(std::string&, int)’:
/home/jwbae/root/include/ROOT/RStringView.hxx:67:29: error: request for member ‘data’ in ‘right’, which is of non-class type ‘int’
   67 |    return left.append(right.data(), right.size());
      |                             ^~~~
/home/jwbae/root/include/ROOT/RStringView.hxx:67:43: error: request for member ‘size’ in ‘right’, which is of non-class type ‘int’
   67 |    return left.append(right.data(), right.size());
      |                                           ^~~~
/home/jwbae/root/include/ROOT/RStringView.hxx: At global scope:
/home/jwbae/root/include/ROOT/RStringView.hxx:84:17: error: expected type-specifier
   84 |        operator std::string_view() const { return std::string_view(fData,fLength); }
      |                 ^~~
In file included from /home/jwbae/root/include/TNamed.h:26,
                 from /home/jwbae/root/include/TDirectory.h:24,
                 from /home/jwbae/root/include/TDirectoryFile.h:25,
                 from /home/jwbae/root/include/TFile.h:28,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/include/RunAction.hh:39,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/src/ActionInitialization.cc:33:
/home/jwbae/root/include/TString.h:115:13: error: expected type-specifier
  115 |    operator std::string_view() const { return std::string_view(Data(),fExtent); }
      |             ^~~
/home/jwbae/root/include/TString.h:280:32: error: ‘string_view’ in namespace ‘std’ does not name a type
  280 |    explicit TString(const std::string_view &sub);
      |                                ^~~~~~~~~~~
/home/jwbae/root/include/TString.h:280:27: note: ‘std::string_view’ is only available from C++17 onwards
  280 |    explicit TString(const std::string_view &sub);
      |                           ^~~
/home/jwbae/root/include/TString.h:317:37: error: ‘string_view’ in namespace ‘std’ does not name a type
  317 |    TString    &operator=(const std::string_view &s);
      |                                     ^~~~~~~~~~~
/home/jwbae/root/include/TString.h:317:32: note: ‘std::string_view’ is only available from C++17 onwards
  317 |    TString    &operator=(const std::string_view &s);
      |                                ^~~
/home/jwbae/root/include/TString.h:444:9: error: ‘string_view’ in namespace ‘std’ does not name a type
  444 |    std::string_view View() const { return std::string_view(GetPointer(),Length()); }
      |         ^~~~~~~~~~~
/home/jwbae/root/include/TString.h:444:4: note: ‘std::string_view’ is only available from C++17 onwards
  444 |    std::string_view View() const { return std::string_view(GetPointer(),Length()); }
      |    ^~~
In file included from /home/jwbae/root/include/TNamed.h:26,
                 from /home/jwbae/root/include/TDirectory.h:24,
                 from /home/jwbae/root/include/TDirectoryFile.h:25,
                 from /home/jwbae/root/include/TFile.h:28,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/include/RunAction.hh:39,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/src/ActionInitialization.cc:33:
/home/jwbae/root/include/TString.h:839:53: error: ‘string_view’ in namespace ‘std’ does not name a type
  839 | inline Bool_t operator==(const char *s1, const std::string_view &s2)
      |                                                     ^~~~~~~~~~~
/home/jwbae/root/include/TString.h:839:48: note: ‘std::string_view’ is only available from C++17 onwards
  839 | inline Bool_t operator==(const char *s1, const std::string_view &s2)
      |                                                ^~~
/home/jwbae/root/include/TString.h:839:15: error: ‘Bool_t operator==(const char*, const int&)’ must have an argument of class or enumerated type
  839 | inline Bool_t operator==(const char *s1, const std::string_view &s2)
      |               ^~~~~~~~
/home/jwbae/root/include/TString.h:844:37: error: ‘string_view’ in namespace ‘std’ does not name a type
  844 | inline Bool_t operator==(const std::string_view &s1, const char *s2)
      |                                     ^~~~~~~~~~~
/home/jwbae/root/include/TString.h:844:32: note: ‘std::string_view’ is only available from C++17 onwards
  844 | inline Bool_t operator==(const std::string_view &s1, const char *s2)
      |                                ^~~
/home/jwbae/root/include/TString.h:844:15: error: ‘Bool_t operator==(const int&, const char*)’ must have an argument of class or enumerated type
  844 | inline Bool_t operator==(const std::string_view &s1, const char *s2)
      |               ^~~~~~~~
/home/jwbae/root/include/TString.h:857:37: error: ‘string_view’ in namespace ‘std’ does not name a type
  857 |   std::string printValue(const std::string_view* val);
      |                                     ^~~~~~~~~~~
/home/jwbae/root/include/TString.h:857:32: note: ‘std::string_view’ is only available from C++17 onwards
  857 |   std::string printValue(const std::string_view* val);
      |                                ^~~
In file included from /home/jwbae/workspace/sim_minimal/sim_minimal/include/RunAction.hh:39,
                 from /home/jwbae/workspace/sim_minimal/sim_minimal/src/ActionInitialization.cc:33:
/home/jwbae/root/include/TFile.h:327:45: error: ‘std::string_view’ has not been declared
  327 |    static Bool_t       SetCacheFileDir(std::string_view cacheDir, Bool_t operateDisconnected = kTRUE,
      |                                             ^~~~~~~~~~~
/home/jwbae/root/include/TFile.h: In static member function ‘static Bool_t TFile::SetCacheFileDir(ROOT::Internal::TStringView, Bool_t, Bool_t)’:
/home/jwbae/root/include/TFile.h:326:36: error: ‘string_view’ is not a member of ‘std’
  326 |      { return SetCacheFileDir(std::string_view(cacheDir), operateDisconnected, forceCacheread); }
      |                                    ^~~~~~~~~~~
/home/jwbae/root/include/TFile.h:326:36: note: ‘std::string_view’ is only available from C++17 onwards
make[2]: *** [CMakeFiles/sim.dir/build.make:76: CMakeFiles/sim.dir/src/ActionInitialization.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/sim.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

You can try to force ROOT and Geant to the same C++ standard version with -DCMAKE_CXX_STANDARD=17 or similar.

Alternatively you can check which standard ROOT was built with (it will appear in the flags printed by root-config --cflags) and force Geant to build with the same standard. Make sure to always start from an empty build directory.

Cheers,
Enrico