Your assumption about Comeau implicitly calling an explicit
constructor is most likely incorrect. The behavior is indeed broken, but the problem is different.
I suspect that this is a bug in the implementation of Standard Library that comes with Comeau, not with core Comeau compiler itself (although the line is blurry in this case).
If you build a quick dummy class that has constructor properties similar to std::vector
and try the same thing, you'll discover that the compiler correctly refuses to perform construction.
The most likely reason why it accepts your code is the well-known formal ambiguity of two-parameter constructor of std::vector
. It can be interpreted as (size, initial value)
constructor
explicit vector(size_type n, const T& value = T(),
const Allocator& = Allocator());
or as (begin, end)
template constructor with the latter accepting two iterators
template <class InputIterator>
vector(InputIterator first, InputIterator last,
const Allocator& = Allocator());
The standard library specification explicitly states that when two integral values are used as arguments, the implementation must make sure somehow that the formed constructor is selected, i.e. (size, initial value)
. How it is done - doesn't matter. It can be done at the library level, it can be hardcoded in the core compiler. It can be done in any other way.
However, in response to ( 20, 40 )
arguments Comeau compiler appears to erroneously select and instantiate the latter constructor with InputIterator = int
. I don't know how it manages to compile the specialized version of the constructor, since integral values can't and won't work as iterators.
If you try this
vector< vector< int > > v( 20U, 40 );
you'll discover that the compiler reports an error now (since it can no longer use the two-iterator version of the constructor) and the explicit
on the first constructor prevents it from converting 40
to a std::vector
.
The same thing happens with assign
. This certainly a defect of Comeau implementation, but, once again, experiments show that most likely the required behavior was supposed to be enforced at the library level (the core compiler seems to work OK), and somehow it got done incorrectly.
On the second thought, I see that the main idea in my explanation is correct, but the details are wrong. Also, I can be wrong about calling it a problem in Comeau. It is possible that Comeau is right here.
The standard says in 23.1.1/9 that
the constructor
template <class InputIterator>
X(InputIterator f, InputIterator l, const Allocator& a = Allocator())
shall have the same effect as:
X(static_cast<typename X::size_type>(f),
static_cast<typename X::value_type>(l),
a)
if InputIterator is an integral type
I suspect that if the above is interpreted literally, the compiler is allowed to assume that an explicit static_cast
is implied there (well... so to say), and the code is legal for the same reason static_cast< std::vector<int> >(10)
is legal, despite the corresponding constructor's being explicit
. The presence of static_cast
is what makes it possible for the compiler to use the explicit
constructor.
If the behavior of Comeau compiler is correct (and I suspect that it is in fact correct, as required by the standard), I wonder whether this was the intent of the committee to leave such a loophole open, and allow implementations to work arount the explicit
restriction possibly present on the constructor of vector element.