Title
Container iterator constructor and explicit convertibility
Status
dup
Section
[container.requirements]
Submitter
Joaquín M López Muñoz

Created on 2005-12-17.00:00:00 last changed 172 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

Duplicate: 589

Date: 2010-10-21.18:28:33

[ Berlin: Some support, not universal, for respecting the explicit qualifier. ]

Date: 2010-10-21.18:28:33

[ The following added by Howard and the example code was originally written by Dietmar. ]

Valid code below?

#include <vector> 
#include <iterator> 
#include <iostream> 

struct foo 
{ 
    explicit foo(int) {} 
}; 

int main() 
{ 
    std::vector<int> v_int; 
    std::vector<foo> v_foo1(v_int.begin(), v_int.end()); 
    std::vector<foo> v_foo2((std::istream_iterator<int>(std::cin)), 
                             std::istream_iterator<int>()); 
} 
Date: 2005-12-17.00:00:00

The iterator constructor X(i,j) for containers as defined in 23.1.1 and 23.2.2 does only require that i and j be input iterators but nothing is said about their associated value_type. There are three sensible options:

  1. iterator's value_type is exactly X::value_type (modulo cv).
  2. iterator's value_type is *implicitly* convertible to X::value_type.
  3. iterator's value_type is *explicitly* convertible to X::value_type.

The issue has practical implications, and stdlib vendors have taken divergent approaches to it: Dinkumware follows 2, libstdc++ follows 3.

The same problem applies to the definition of insert(p,i,j) for sequences and insert(i,j) for associative contianers, as well as assign.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg2986
2010-10-21 18:28:33adminsetmessages: + msg2985
2010-10-21 18:28:33adminsetmessages: + msg2984
2005-12-17 00:00:00admincreate