I like to initialize 2-dimensional arrays as vector<vector<int> >(x,y). x is passed to vector<vector<int> >‘s constructor and y is passed to vector<int>‘s constructor, x times. Although this seems to be forbidden by C++03, because the constructor is explicit, it always works, even on Comeau. I can also call vector::assign like this. But, for some reason, not vector::push_back.
vector< vector< int > > v( 20, 40 ); // OK: convert 40 to const T&
v.assign( 30, 50 ); // OK
v.push_back( 2 ); // error: no conversion, no matching function, etc.
Are the first two examples actually compliant for some reason? Why can I convert 40 and 50 but not 2?
Epilogue: see http://gcc.gnu.org/onlinedocs/libstdc++/ext/lwg-defects.html#438 for why most compilers allow this, but the standard is shifting the other way.
Your assumption about Comeau implicitly calling an
explicitconstructor 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::vectorand 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)constructoror as
(begin, end)template constructor with the latter accepting two iteratorsThe 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 withInputIterator = 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
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
expliciton the first constructor prevents it from converting40to astd::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
I suspect that if the above is interpreted literally, the compiler is allowed to assume that an explicit
static_castis implied there (well… so to say), and the code is legal for the same reasonstatic_cast< std::vector<int> >(10)is legal, despite the corresponding constructor’s beingexplicit. The presence ofstatic_castis what makes it possible for the compiler to use theexplicitconstructor.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
explicitrestriction possibly present on the constructor of vector element.