views:

237

answers:

4

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.

+1  A: 

Are the first two examples actually compliant for some reason?

They are not compliant on the compiler I just tried. (gcc 4.4.1)

Why can I convert 40 and 50 but not 2?

Since the first two lines are not consistent with the standard, only Comeau may know why their inconsistency is inconsistent.

It is not an accident that the standard requires explicit conversions from int types to arbitrary vectors. It is done to prevent confusing code.

Shmoopty
OK. They work on GCC 4.2.1 and I've never seen it not work (past GCC and Metrowerks). The inconsistency is the same between GCC 4.2.1 and Comeau test drive. Since Comeau usually tries to reject noncompliant code in strict mode, I've asked them. Although the idiom is a lil confusing at first, it's a lot nicer than writing out the templated type…
Potatoswatter
A compiler does not determine compliance. Perhaps the code is not *accepted* by gcc, but leiz and AndreyT explained how the Comeau behavior is required by the standard, so the code is compliant and gcc is not.
Ben Voigt
@Ben Voigt, I am referring to the compiler being compliant with the code. Indeed, using a compiler it is not a test for "C++ standard compliance".
Shmoopty
A: 

Your examples aren't compliant. The constructor is explicit as you said, so you not allowed to pass an int (y) instead of a vector for the constructor (and y is not passed "x times" to vector constructor: the second paramater is only created once, to initilialize the inserted objects). Your examples don't work under gcc (4.4 & 4.5).

rafak
+5  A: 

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.

AndreyT
The behavior is by design because the ambiguity of using `vector<int>::vector(20,20)` for example. It has to specialize for int iterator to work because 20 here is int not unsigned int. So the compiler will select `vector(iterator,iterator)` version. But the behavior you want is obvious the `vector(size_t,T)`
leiz
@leiz: As I said in my answer, the C++ standard explcitly requires implementations to interpret integral arguments as `size, value` pair. If Comeau failes to interpret them that way, it a potential problem in Comeau, not anything "by design".
AndreyT
When you provide parameters (20,20) to the constructor, the compiler has to find the best match, right? Because `vector(size_t,T)` sees a int instead of unsigned int, it best choice is `template<class iterator> vector(iterator,iterator)` right? So library writer has to work around it providing a specialized version for int to work like `vector(size_t,T)`. Please carefully differentiate between library design and compiler behavior.
leiz
@leiz: There's no clear separation between core language compiler and the standard library. As I said in my answer, the required behavior can be implemented on the library level as well as hardcoded in the compiler. Library code is not a general user-level code. It has special status. In fact, library code might not exist at all: it might be integrated/hardcoded into the compiler. In other words, there no clear differentiation between the library and the compiler, which is why your request for "carefull differentiation" makes no sense in general case.
AndreyT
I disagree entirely. Compiler and library is totally different entities. What you just said looks like a hack to me. Compiler should never knows anything about a library even if it is standard library defined in the standard.
leiz
@leitz: Compiler and library cannot be totaly different entities. There are several obvious points where they cross into each other's territory. For example, `typeid` operator returns a reference to a library type `std::type_info`. `dynamic_cast` can throw `std::bad_cast`, which inherits from `std::exception` and so on. If standard library was an independent entity, there wouldn't be a single legal way to implement the `offsetof` macro. Moreover, the very concept of the standard library, as described in the language standard, does not allow it to be a "totally different entity".
AndreyT
@Andrey: The compiler and library can be distinct in most areas, including this one. My code might not work if they had, for example, said the behavior of vector::vector(int,int) is the same a call to a hypothetical vector::assign_fill(size_type,T).
Potatoswatter
+1  A: 

vector< vector< int > > v( 20, 40 ); use a constructor that you might not be familiar with. The constructor is called here is vector(iterator start, iterator end);

Internally, it specializes to an int iterator, so the first parameter is treated as count, and second parameter is the value to initialize the vector. Because there is a cast when assign the second parameter to a vector value, so the constructor of the inner vector<T>(int, const T&) will be called with value 40. Therefore, the inner vector is constructed with 40 0's.

leiz