Allow comparing wxList::compatibility_iterator with nullptr

This doesn't cost anything but allows the old code which relied on
compatibility_iterator being implicitly convertible to a pointer (and
hence comparable with nullptr) continue to compile and work.

Add a unit test checking that this works as expected.
This commit is contained in:
Vadim Zeitlin
2026-02-06 14:24:08 +01:00
parent 6af0731a84
commit 88b179b672
2 changed files with 29 additions and 0 deletions

View File

@@ -36,6 +36,7 @@
#if wxUSE_STD_CONTAINERS
#include "wx/beforestd.h"
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <list>
#include "wx/afterstd.h"
@@ -121,6 +122,11 @@ public:
bool operator !() const
{ return !( operator bool() ); }
bool operator==(std::nullptr_t) const
{ return !*this; }
bool operator!=(std::nullptr_t) const
{ return !(*this == nullptr); }
elT GetData() const
{ return *m_iter; }
void SetData( elT e )

View File

@@ -194,6 +194,29 @@ TEST_CASE("wxList::ctor", "[list]")
CHECK( Baz::GetNumber() == 0 );
}
TEST_CASE("wxList::iterator::cmp", "[list]")
{
int dummy[2];
wxListInt list;
list.push_back(dummy);
list.push_back(dummy + 1);
wxListInt::compatibility_iterator it = list.GetFirst();
CHECK( it == it );
CHECK( it != nullptr );
const wxListInt::compatibility_iterator last = list.GetLast();
CHECK( last != nullptr );
CHECK( it != last );
it = list.Item(1);
CHECK( it == last );
it = list.Find(dummy + 2);
CHECK( it == nullptr );
}
// Check for WX_DECLARE_LIST_3 which is used to define wxWindowList: we can't
// use this class itself here, as it's in the GUI library, so declare something
// similar.