首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在CString中使用C++20时缺少MFC operator==()重载(17.0.6)

在CString中使用C++20时缺少MFC operator==()重载(17.0.6)
EN

Stack Overflow用户
提问于 2022-02-02 21:11:04
回答 1查看 334关注 0票数 5

更新:这是在VS2022 v17.2预览1中修复的

当我将MFC项目设置为使用C++20时,在将字符串文本与CString实例进行比较时会出现错误。

例如:

代码语言:javascript
运行
复制
CString s1 = _T("s1");
    
// this works
if (s1 == _T("s1")) {
  cout << "Match!"
}

// this generates a compiler error 
if (_T("s1") == s1) {
  cout << "Match!";
}

根据CStringT文档,有一堆==重载:

代码语言:javascript
运行
复制
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();

我标记了我认为应该找到的那个。然而,它似乎甚至没有考虑到这种过载:

代码语言:javascript
运行
复制
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函数,包括我标记的函数,都是#ifdefd,因为__cpp_lib_three_way_comparison#define'd。

太空船操作员是否不知何故使这些朋友的功能变得不必要?如果是的话,解决办法是什么?我知道我可以将我的比较改为s1 == _T("s1"),但我喜欢左边有一个常量表达式,以防我不小心使用=而不是==

EN

回答 1

Stack Overflow用户

发布于 2022-02-25 17:35:57

根据https://developercommunity.visualstudio.com/t/Missing-comparison-operators-between-LPC/1614285的说法,微软在17.2预览1中修正了这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70962914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档