Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#15216 closed defect (fixed)

Missing constructor from a pair of iterators in wxArray STL build and wxVector

Reported by: troelsk Owned by:
Priority: normal Milestone:
Component: base Version: stable-latest
Keywords: wxArray wxArrayInt wxVector simple Cc:
Blocked By: Blocking:
Patch: yes


This compiles with wxUSE_STL==0, both in trunk and 2.8
But with wxUSE_STL==1, it fails to compile, no such ctor

    const int init[] = { 11, 22, 33 };
    wxArrayInt array(init, init + WXSIZEOF(init));

    for (wxArrayInt::const_iterator it = array.begin(); it != array.end(); it++)
        printf("%d", *it);

Attachments (1)

ctor.patch download (1.6 KB) - added by troelsk 5 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 5 years ago by vadz

  • Keywords wxVector simple added
  • Status changed from new to confirmed
  • Summary changed from Missing wxArray constructor to Missing constructor from a pair of iterators in wxArray STL build and wxVector

Yes, we need a ctor from 2 iterators in the STL case too.

BTW, wxVector<> doesn't have this ctor neither.

comment:2 Changed 5 years ago by troelsk

I can't get it to work, the compiler keeps picking the wrong ctor,

1>.\docview.cpp(172) : error C2664: 'wxArrayInt::wxArrayInt(unsigned int,const int &)' : cannot convert parameter 1 from 'const int [3]' to 'unsigned int'

classexp name : public std::vector<T>                               \
{                                                                   \
   name() : std::vector<T>() { }                                     \
   name(size_type n) : std::vector<T>(n) { }                         \
   name(size_type n, const_reference v) : std::vector<T>(n, v) { }   \
+  name(const_iterator first, const_iterator last) : std::vector<T>(first, last) { }   \

comment:3 Changed 5 years ago by vadz

The ctor must be a template function taking an arbitrary iterator, not just const_iterator. I.e. something like this (warning, totally untested) should work:

classexp name : public std::vector<T> {
    template <class InputIterator>
    name(InputIterator first, InputIterator last) : std::vector<T>(first, last) { }

Changed 5 years ago by troelsk

comment:4 Changed 5 years ago by troelsk

  • Patch set

Thanks! Patch added for wxArray and wxVector

comment:5 Changed 5 years ago by vadz

I'll apply this soon but wxVector version is wrong, we can't subtract two input iterators in general (think about iterators for accessing input stream contents). And we can't assign them to const_iterator neither.

The best would be to use template dispatch to call reserve() if possible, i.e. if the iterators are, in fact, random access ones. But I don't have time for this right now...

comment:6 Changed 5 years ago by VZ

  • Resolution set to fixed
  • Status changed from confirmed to closed

(In [74044]) Add ctor from two input iterators to wxArray in STL build too.

This ctor already existed in non-STL build but was omitted from the STL

Closes #15216.

comment:7 Changed 5 years ago by VZ

(In [74045]) Add ctor and assign() taking an iterator range to wxVector<>.

Do it for consistency with wxArray and std::vector<>, even if the current
implementation is suboptimal.

See #15216.

Note: See TracTickets for help on using tickets.