Are there any real benefits for me to migrate my useage of CMapStringToString
over to std::map<CString, CString>
in my application?
I don't understand, I tried to change the code and now it won't compile:
7>------ Build started: Project: Meeting Schedule Assistant, Configuration: Release x64 ------
7>CalendarSettingsOutlookPage.cpp
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(112,53): error C2064: term does not evaluate to a function taking 1 arguments
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(135): message : see reference to variable template 'const bool _Nothrow_hash<std::hash<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >' being compiled
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(135): message : while compiling class template member function 'size_t std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::operator ()<_Kty>(const _Keyty &) noexcept(<expr>) const'
7> with
7> [
7> _Kty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Keyty=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>
7> ]
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(1109): message : see reference to variable template 'const bool _Nothrow_hash<std::_Umap_traits<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::_Uhash_compare<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::hash<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::equal_to<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,0>,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >' being compiled
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(1096): message : while compiling class template member function 'void std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::clear(void) noexcept'
7> with
7> [
7> _Kty=CString,
7> _Ty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Alloc=std::allocator<std::pair<const CString,CString>>
7> ]
7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\CalendarSettingsOutlookPage.cpp(156): message : see reference to function template instantiation 'void std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::clear(void) noexcept' being compiled
7> with
7> [
7> _Kty=CString,
7> _Ty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Alloc=std::allocator<std::pair<const CString,CString>>
7> ]
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\unordered_map(67): message : see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
7> with
7> [
7> _Kty=CString,
7> _Ty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Alloc=std::allocator<std::pair<const CString,CString>>
7> ]
7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\CalendarSettingsOutlookPage.h(59): message : see reference to class template instantiation 'std::unordered_map<CString,CString,std::hash<CString>,std::equal_to<CString>,std::allocator<std::pair<const CString,CString>>>' being compiled
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(111,44): error C2056: illegal expression
7>Done building project "Meeting Schedule Assistant.vcxproj" -- FAILED.
If I use std::map<CString, CString>
it works but std::unordered_map<CString, CString>
throws out the errors above. My usage calls:
if (m_mapCalendarList.find(strCalendarName) != m_mapCalendarList.end())
if (strCalendarId == m_mapCalendarList[strCalendarName])
m_mapCalendarList.clear();
The variable is defined in the header.
MFC hash maps have some drawbacks, except from just being not C++ Standard and not portable:
For std::unordered_map
, you need to implement hash function, and pass it as the corresponding template parameter. Or specialize standard hash for CString
. But I prefer the former option.
And would be good to pass also comparison function, that performs locale-independent comparison. Since with a hash that is not aware of locale, you can't have locale-aware map anyway, but CString::operator==
is locale-aware.
As there's HashKey
that can take LPCSTR
and LPCWSTR
that is used by CMap
, implementation of own STL-compatible hashes is trivial.
You can completely avoid dealing with hashes, and use std::unordered_map<std::string, std::string>
. But transition to std::string
will cause way more changes I guess.