更新:这在VS2022 v17.2预览版1中得到修复
当我将MFC项目设置为使用C++20时,在将字符串文字与CString
实例进行比较时会出现错误。
例如:
CString s1 = _T("s1");
// this works
if (s1 == _T("s1")) {
cout << "Match!"
}
// this generates a compiler error
if (_T("s1") == s1) {
cout << "Match!";
}
根据CStringT
文档,存在一堆==
过载:
friend bool operator==(const CStringT& str1, const CStringT& str2) throw();
friend bool operator==(const CStringT& str1, PCXSTR psz2) throw();
friend bool operator==(const CStringT& str1, PCYSTR psz2) throw();
friend bool operator==(const CStringT& str1, XCHAR ch2) throw();
-> friend bool operator==(PCXSTR psz1, const CStringT& str2) throw();
friend bool operator==(PCYSTR psz1, const CStringT& str2,) throw();
friend bool operator==(XCHAR ch1, const CStringT& str2,) throw();
我标记了我认为应该找到的那个。然而,它似乎甚至没有考虑到过载:
error C2666: '==': 3 overloads have similar conversions
message : could be 'bool operator ==(const D2D1_RECT_U &,const D2D1_RECT_U &)'
message : or 'bool operator ==(const D2D1_SIZE_U &,const D2D1_SIZE_U &)'
message : or 'bool operator ==(const DEVPROPCOMPKEY &,const DEVPROPCOMPKEY &)'
message : or 'bool operator ==(const DEVPROPKEY &,const DEVPROPKEY &)'
message : or 'int operator ==(const PROPERTYKEY &,const PROPERTYKEY &)'
message : or 'bool operator ==(const GUID &,const GUID &)'
message : or 'bool ATL::operator ==(const 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>>> &) noexcept' [found using argument-dependent lookup]
message : or 'bool ATL::operator ==(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &,const wchar_t *) noexcept' [found using argument-dependent lookup]
message : or 'bool ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::operator ==(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &,const char *)' [found using argument-dependent lookup]
message : or 'bool ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::operator ==(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &,wchar_t) noexcept' [found using argument-dependent lookup]
message : or 'bool operator ==(const GUID &,const GUID &)' [synthesized expression 'y == x']
message : or 'bool ATL::operator ==(const 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>>> &) noexcept' [synthesized expression 'y == x']
message : or 'bool ATL::operator ==(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &,const wchar_t *) noexcept' [synthesized expression 'y == x']
message : or 'bool ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::operator ==(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &,const char *)' [synthesized expression 'y == x']
message : or 'bool ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::operator ==(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &,wchar_t) noexcept' [synthesized expression 'y == x']
message : or 'int operator ==(const PROPERTYKEY &,const PROPERTYKEY &)' [synthesized expression 'y == x']
message : or 'bool operator ==(const DEVPROPKEY &,const DEVPROPKEY &)' [synthesized expression 'y == x']
message : or 'bool operator ==(const DEVPROPCOMPKEY &,const DEVPROPCOMPKEY &)' [synthesized expression 'y == x']
message : or 'bool operator ==(const D2D1_SIZE_U &,const D2D1_SIZE_U &)' [synthesized expression 'y == x']
message : or 'bool operator ==(const D2D1_RECT_U &,const D2D1_RECT_U &)' [synthesized expression 'y == x']
message : or 'built-in C++ operator==(const wchar_t [3], const wchar_t [3])'
message : or 'built-in C++ operator==(const wchar_t *, const wchar_t *)'
message : while trying to match the argument list '(const wchar_t [3], CString)'
所以我转到头文件(cstringt.h
(,发现一堆friend
函数,包括我标记的那个,都是#ifdef
'd out,因为__cpp_lib_three_way_comparison
是#define
'd。
宇宙飞船操作员是不是在某种程度上让一堆朋友的功能变得不必要了?如果是,解决方法是什么?我知道我可以将比较更改为s1 == _T("s1")
,但我喜欢在左边有一个常量表达式,以防我意外使用=
而不是==
。
这是微软在17.2预览版1中根据https://developercommunity.visualstudio.com/t/Missing-comparison-operators-between-LPC/1614285.