Allow swapping wxString iterators in C++11 mode

Specialize std::iter_swap() for wxString::iterator in C++11 mode as the
default implementation of it doesn't work for iterators using "reference" type
different from "value_type&" as is the case for wxString::iterator.

This notably allows the code using std::reverse() with wxString iterators to
compile in C++11 mode, which wasn't the case before.
This commit is contained in:
Vadim Zeitlin
2016-07-14 16:36:24 +02:00
parent 0e541e7a57
commit 14ec2691f6
2 changed files with 34 additions and 0 deletions

View File

@@ -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>(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
// ---------------------------------------------------------------------------

View File

@@ -20,6 +20,8 @@
#include "wx/wx.h"
#endif // WX_PRECOMP
#include <algorithm>
// ----------------------------------------------------------------------------
// 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 );
}