Don't clobber std::string_view equality with char *
Make the wxString(std::string_view) constructor explicit.
Otherwise, when comparing a std::string_view with a const char *, the
cast to wxString will be considered as a candidate for the comparison,
ultimately causing an "ambiguous overload for 'operator=='" error.
For example, this sample only builds if the constructor is explicit:
#include <wx/string.h>
#include <string_view>
int main() {
std::string_view view = "abc";
const char *str = "abc";
return view == str;
}
However, making the constructor explicit will break assignment:
std::string_view view = "abc";
wxString s;
s = view; // Error: no match for "operator="
That we can fix by implementing operator=(std::string_view)
That, however, introduces another ambiguity:
std::string str = "abc";
wxString s;
s = str; // Ambiguous between s = wxString(str)
and s = std::string_view(str)
That we can fix by implementing operator=(std::string)
Finally, note that some rather obscure ambiguities remain, such as:
wxString s;
s = {"abc", 2}; // Ambiguous between s = wxString("abc", 2)
and s = std::string_view("abc", 2)
Avoiding them is not simple (https://cplusplus.github.io/LWG/issue2946)
and doesn't add much value.
Closes #23834.
This commit is contained in:
parent
b3cfab1433
commit
19936c2176
3 changed files with 39 additions and 2 deletions
|
|
@ -690,5 +690,14 @@ TEST_CASE("StdString::View", "[stdstring]")
|
|||
std::string_view strViewInvalidUTF(strInvalidUTF);
|
||||
|
||||
CHECK( "" == wxString::FromUTF8(strViewInvalidUTF) );
|
||||
|
||||
/* Ensure we don't clobber comparisons on base types */
|
||||
std::string_view view = "abc";
|
||||
const char *str = "abc";
|
||||
CHECK( view == str );
|
||||
|
||||
std::wstring_view wview = L"abc";
|
||||
const wchar_t *wstr = L"abc";
|
||||
CHECK( wview == wstr );
|
||||
}
|
||||
#endif // wxHAS_STD_STRING_VIEW
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue