diff --git a/include/wx/string.h b/include/wx/string.h index 7eb6f4938c..9e1db371be 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -3986,6 +3986,29 @@ namespace std #endif // C++11 +// Specialize std::iter_swap in C++11 to make std::reverse() work with wxString +// iterators: unlike in C++98, where iter_swap() is required to deal with the +// iterator::reference being different from "iterator::value_type&", in C++11 +// iter_swap() just calls swap() by default and this doesn't work for us as +// wxUniCharRef is not the same as "wxUniChar&". +#if __cplusplus >= 201103L + +namespace std +{ + template <> + inline void + iter_swap(wxString::iterator i1, wxString::iterator i2) + { + // We don't check for i1 == i2, this won't happen in normal use, so + // don't pessimize the common code to account for it. + wxUniChar tmp = *i1; + *i1 = *i2; + *i2 = tmp; + } +} // namespace std + +#endif // C++11 + // --------------------------------------------------------------------------- // Implementation only from here until the end of file // --------------------------------------------------------------------------- diff --git a/tests/strings/stdstrings.cpp b/tests/strings/stdstrings.cpp index 0110e9d0e8..5c13f9bf44 100644 --- a/tests/strings/stdstrings.cpp +++ b/tests/strings/stdstrings.cpp @@ -20,6 +20,8 @@ #include "wx/wx.h" #endif // WX_PRECOMP +#include + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- @@ -50,6 +52,7 @@ private: #if wxUSE_STD_STRING CPPUNIT_TEST( StdConversion ); #endif + CPPUNIT_TEST( StdAlgo ); CPPUNIT_TEST_SUITE_END(); void StdConstructors(); @@ -71,6 +74,7 @@ private: #if wxUSE_STD_STRING void StdConversion(); #endif + void StdAlgo(); wxDECLARE_NO_COPY_CLASS(StdStringTestCase); }; @@ -617,3 +621,10 @@ void StdStringTestCase::StdConversion() CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(s11), "" ); } #endif // wxUSE_STD_STRING + +void StdStringTestCase::StdAlgo() +{ + wxString s("AB"); + std::reverse(s.begin(), s.end()); + CPPUNIT_ASSERT_EQUAL( "BA", s ); +}