Title
Problematic iterator-pair constructor of containers
Status
open
Section
[c.strings]
Submitter
Johannes Schaub

Created on 2013-02-02.00:00:00 last changed 119 months ago

Messages

Date: 2014-06-28.17:58:16

[ 2014-06 Rapperswil ]

JW: can't fix this, don't want to touch this, Do The Right Thing clause has been a source of tricky issues. only really happens with string literals, that's the only way to create an array that isn't obviously an array

GR: want to see paper

AM: is it only string literals, or also UDLs?

STL: maybe, but we don't need to deal with that. This is only a problem in a very specific case

Leave as Open.

Date: 2016-08-09.17:32:48

The non-explicit nature of the iterator-pair constructor of containers, such a

template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());

can be selected in unexpected situations, leading to a hard runtime error, as demonstrated by the following example:

#include <vector>

void f(std::vector<char> v){ /* ... */}

int main() {
  f({"A", "B"});
}

The actually intended initializer-list constructor isn't feasible here, so the best match is the constructor template

template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());

This compiles, but will result in code running amok. The potential trap (that cannot be easily detected by the library implementation) could be reduced by making this constructor explicit. It would still have the effect to be selected here, but the code would be ill-formed, so the programmer gets a clear message here.

History
Date User Action Args
2014-06-28 17:58:16adminsetmessages: + msg7076
2014-06-28 17:58:16adminsetstatus: new -> open
2013-02-02 00:00:00admincreate