Std::unordered_map with TString

Hello All,

I was wondering if it were possible to use std::unordered_map with TString.

It seems that statements like

 std::unordered_map <Int_t,int> aMap_1; 

are acceptable, while something like:

 std::unordered_map <TString,int> aMap_2; 

can not be compiled.

The error message I get is:

clang++ -O2 -pipe -Wall -W -Woverloaded-virtual -pthread -stdlib=libc++ -std=c++11 -m64 -I/Users/sadeh/work/root/root/include -std=c++0x   -c -o myMain.o myMain.cpp
In file included from myMain.cpp:1:
In file included from ./myMain.hpp:1:
In file included from /Users/sadeh/work/root/root/include/TROOT.h:29:
In file included from /Users/sadeh/work/root/root/include/TDirectory.h:25:
In file included from /Users/sadeh/work/root/root/include/TNamed.h:26:
In file included from /Users/sadeh/work/root/root/include/TObject.h:31:
In file included from /Users/sadeh/work/root/root/include/Rtypes.h:30:
In file included from /Users/sadeh/work/root/root/include/Rtypeinfo.h:32:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/typeinfo:61:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/exception:81:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:948:38: error: implicit instantiation of undefined template
      'std::__1::hash<TString>'
    : public integral_constant<bool, __is_empty(_Tp)> {};
                                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:362:54: note: in instantiation of template class
      'std::__1::is_empty<std::__1::hash<TString> >' requested here
template <class _Key, class _Cp, class _Hash, bool = is_empty<_Hash>::value
                                                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:731:13: note: in instantiation of default argument for
      '__unordered_map_hasher<TString, std::__1::__hash_value_type<TString, int>, std::__1::hash<TString> >' required here
    typedef __unordered_map_hasher<key_type, __value_type, hasher>   __hasher;
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myMain.cpp:20:36: note: in instantiation of template class 'std::__1::unordered_map<TString, int, std::__1::hash<TString>, std::__1::equal_to<TString>,
      std::__1::allocator<std::__1::pair<const TString, int> > >' requested here
  std::unordered_map <TString,int> aMap_2;
                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:3065:29: note: template is declared here
template <class _Tp> struct hash;
                            ^
1 error generated.
make: *** [myMain.o] Error 1

I am working on Mac OSX (Yosemite 10.10.4), using ROOT v6-04-02
(similar problems also occurred on a SL Linux machine with ROOT v5.34/14.)

A minimal example is attached (uncomment line 16 in myMain.cpp to compile w/o errors).

Any advice would be appreciated.
Thanks,

Iftach Sadeh.
minExample.tgz (1.25 KB)

Hi,

have a look at the definition of std::unordered_map:

en.cppreference.com/w/cpp/contai … rdered_map

template<
class Key,
class T,
class Hash = std::hash,
class KeyEqual = std::equal_to,
class Allocator = std::allocator< std::pair<const Key, T> >

class unordered_map;

If you use a non-standard type like TString as a key, you have to provide hashing and equal functions to the map.

Alternatively, you could look at TMap of Root, which should work with TObjString.

Cheers,
Jochen

Thank you very much, Jochen!

For those who are interested, adding the following to the .hpp file did the trick:

#include <unordered_map>

namespace std {

  template <>
  struct hash<TString> {
    std::size_t operator()(const TString & k) const {
      return std::hash<std::string>()(k.Data());
    };
  };
  
}

Best,
Iftach.