diff --git a/docs/changes.txt b/docs/changes.txt index 174af147fa..624767ba25 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -108,6 +108,11 @@ Changes in behaviour which may result in build errors compatible with the previous wxWidgets versions, but now compare values, and not pointers, in their Index() member function. +- Due to the possibility to construct wxString from std::string_view some + previously valid code, such as "wxstr = {"Hello", 2}", is now ambiguous. + Please use explicit class name, e.g. "wxstr = wxString{"Hello", 2}" to + preserve the previous behaviour. + - Generic wxSearchCtrl doesn't provide methods that make sense only for multiline text controls any longer, for consistency with the other ports. diff --git a/include/wx/string.h b/include/wx/string.h index be59395409..a324feef06 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1263,7 +1263,7 @@ public: #endif #ifdef wxHAS_STD_STRING_VIEW - wxString(std::wstring_view view) + explicit wxString(std::wstring_view view) { assign(view.data(), view.length()); } #endif // wxHAS_STD_STRING_VIEW @@ -1272,7 +1272,7 @@ public: { assign(str.c_str(), str.length()); } #ifdef wxHAS_STD_STRING_VIEW - wxString(std::string_view view) + explicit wxString(std::string_view view) { assign(view.data(), view.length()); } #endif // wxHAS_STD_STRING_VIEW #endif // wxNO_IMPLICIT_WXSTRING_ENCODING @@ -1883,6 +1883,29 @@ public: { return assign(s); } #endif // wxNO_IMPLICIT_WXSTRING_ENCODING +#if wxUSE_UNICODE_WCHAR + wxString& operator=(const std::wstring& str) { m_impl = str; return *this; } + wxString& operator=(std::wstring&& str) noexcept { m_impl = std::move(str); return *this; } +#else // wxUSE_UNICODE_UTF8 + wxString& operator=(const std::wstring& str) + { return assign(str.c_str(), str.length()); } +#endif + +#ifdef wxHAS_STD_STRING_VIEW + wxString& operator=(std::wstring_view view) + { return assign(view.data(), view.length()); } +#endif // wxHAS_STD_STRING_VIEW + +#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING + wxString& operator=(const std::string& str) + { return assign(str.c_str(), str.length()); } + + #ifdef wxHAS_STD_STRING_VIEW + wxString& operator=(std::string_view view) + { return assign(view.data(), view.length()); } + #endif // wxHAS_STD_STRING_VIEW +#endif // wxNO_IMPLICIT_WXSTRING_ENCODING + // string concatenation // in place concatenation /* diff --git a/tests/strings/stdstrings.cpp b/tests/strings/stdstrings.cpp index f3ff37f2ee..c695dd21fc 100644 --- a/tests/strings/stdstrings.cpp +++ b/tests/strings/stdstrings.cpp @@ -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