简体   繁体   English

对重载函数 find_first_not_of 的不明确调用

[英]ambiguous call to overloaded function find_first_not_of

error C2668: 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator&lt:wchar_t>>::find_first_not_of' : ambiguous call to overloaded function错误 C2668:“std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator&lt:wchar_t>>::find_first_not_of”:对重载函数的调用不明确

I get this error in Visual Studio 2013, but not in MinGW.我在 Visual Studio 2013 中收到此错误,但在 MinGW 中没有。 What can I do with this?我能用这个做什么?

I tried adding cache.std::wstring::find_first_not_of , but this did not help.我尝试添加cache.std::wstring::find_first_not_of ,但这没有帮助。

My code:我的代码:

wstring cache = key.GetExpandStringValue(L"Path");
int empregnul = 0;
if(cache.find_first_not_of('\0') == wstring::npos)
{
    empregnul = 1;
}

I think MSVC is not standard-conform by rejecting your code, at least since C++14.我认为 MSVC 拒绝您的代码不符合标准,至少从 C++14 开始。 You are using a version that is older than C++14 and therefore clearly doesn't support it, but later MSVC versions still produce the same ambiguity error, even with /std:c++14 or /std:c++latest and /permissive- flag given, see hhttps://godbolt.org/z/fQA4YD.您使用的版本早于 C++14,因此显然不支持它,但更高的 MSVC 版本仍然会产生相同的歧义错误,即使使用/std:c++14/std:c++latest/permissive-标志,请参阅 hhttps://godbolt.org/z/fQA4YD。 But also note the comment at the end of this answer about more recent versions of MSVC.但还要注意本答案末尾关于 MSVC 更新版本的评论。

MSVC seems to think that '\\0' can be converted to a null pointer value , because it is a null pointer constant . MSVC 似乎认为'\\0'可以转换为空指针值,因为它是空指针常量

This would make the overload of find_first_not_of taking wchar_t as argument and the overload taking const wchar_t* as argument equally good matches.这将使find_first_not_ofwchar_t作为参数的重载和以const wchar_t*作为参数的重载同样匹配。

You can see that this is MSVC's reasoning for the ambiguity by changing the character value to anything but zero, eg 'a' , in which case MSVC thinks the call is unambiguous.通过将字符值更改为除零以外的任何值,您可以看到这是 MSVC 对歧义的推理,例如'a' ,在这种情况下,MSVC 认为调用是明确的。

However, after the resolution of CWG issue 903 as defect report, the relevant passage in [conv.ptr]/1 of the C++14 standard (final draft) and later standards say:但是,在将CWG 问题 903作为缺陷报告解决后,C++14 标准(最终草案)和更高标准的[conv.ptr]/1中的相关段落说:

A null pointer constant is an integer literal ([lex.icon]) with value zero or a prvalue of type std::nullptr_t.空指针常量是具有零值的整数文字 ([lex.icon]) 或类型为 std::nullptr_t 的纯右值。

'\\0' is a character literal , not an integer literal . '\\0'字符文字,而不是整数文字 Therefore it is not a null pointer constant and cannot be converted to a pointer, making the call unambiguous.因此,它不是空指针常量,不能转换为指针,从而使调用明确无误。

Before the resolution of CWG issue 903 any rvalue constant expression of integer type and value zero was a null pointer constant, so in that case MSVC is correct to give the ambiguity.在 CWG 问题 903 解决之前,任何整数类型和零值的右值常量表达式都是空指针常量,因此在这种情况下,MSVC 给出了歧义是正确的。 But that does not explain the behavior in newer versions and with C++14 (or higher) flags.但这并不能解释较新版本和 C++14(或更高版本)标志中的行为。

This can be resolved by using L'\\0' instead of '\\0' , because L'\\0' has type wchar_t , so that it matches the overload expecting wchar_t exactly , while the overload expecting const wchar_t* would require a conversion (assuming the conversion is even allowed), making the former a better match and resolving the ambiguity.这可以通过使用L'\\0'而不是'\\0' ,因为L'\\0'具有wchar_t类型,因此它完全匹配期望wchar_t的重载,而期望const wchar_t*的重载需要转换(假设甚至允许转换),使前者更好地匹配并解决歧义。

The fix mentioned above is also what you should do anyway, even if there wasn't an error (although it doesn't matter in this specific case).上面提到的修复也是您无论如何都应该做的,即使没有错误(尽管在这种特定情况下无关紧要)。 You don't want to mix char with wchar_t .您不想将charwchar_t混合。 When working with wchar_t / wstring always add the L in front of character literals and string literals to give them the correct type.使用wchar_t / wstring始终在字符文字和字符串文字前面添加L以赋予它们正确的类型。


As pointed out by @RaymondChen in a comment under this question, more recent versions of MSVC (that are not available on godbolt) do implement the resolution of CWG 903 when the /permissive- flag is given, see Implicit conversion of integral constant expressions to null pointer .正如@RaymondChen 在此问题下的评论中指出的那样,当给出/permissive-标志时,MSVC 的更新版本(在 Godbolt 上不可用)确实实现了 CWG 903 的分辨率,请参阅整数常量表达式的隐式转换为空指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM