重载wstring和wchar_t *
的类似转换
我有以下代码:
inline bool match(const std::wstring & text1, const std::wstring & text2)
{
return match(text1.c_str(), text2.c_str());
}
inline bool match(const std::wstring & text1, const wchar_t * text2)
{
return match(text1.c_str(), text2);
}
inline bool match(const wchar_t * text1, const std::wstring & text2)
{
return match(text1, text2.c_str());
}
inline bool match(const wchar_t * text1, const wchar_t * text2)
{
return !wcscmp(text1, text2);
}
我得到了:
error C2666: 'match' : 3 overloads have similar conversions
1> could be 'bool match(const wchar_t *,const std::wstring &)'
1> or 'bool match(const std::wstring &,const wchar_t *)'
1> or 'bool match(const std::wstring &,const std::wstring &)'
wstring和wchar_t *之间不应该有任何隐式转换(应该吗?),为什么这些含糊不清?
先感谢您
没有找到相关结果
已邀请:
3 个回复
扫窟
有一个转换构造函数,允许将
隐式转换为
。这是造成歧义的部分原因,尽管真正的问题是重载的排序。 虽然在前三次重载中任何对
的调用都不会调用第四次重载,但第三次重载中的调用只有一个模糊性。原因如下:
(1)中对
的调用没有歧义,因为在该点只有一个名为
的函数可见。 (2)中对
的调用没有歧义,因为(2)与参数的匹配比(1)更好: 要调用(2),第一个参数需要调用
转换构造函数,第二个参数是完全匹配。 要调用(1),需要为两个参数调用转换构造函数。 (3)出现歧义,因为三个可用的重载都不是“最佳”: 要调用(1),需要为两个参数调用转换构造函数。 要调用(2),需要为第一个参数调用转换构造函数,而第二个参数是完全匹配。 要调用(3),第一个参数是完全匹配,但需要为第二个参数调用转换构造函数。 这三者中没有一个明显优于其他两个。 如果移动(4)在其他重载之前被声明,则对于在(1),(2)和(3)中进行的调用将是明确更好的匹配,因为在所有三种情况下两个参数都是完全匹配的。
吐兄
香腔弥胯瓤