Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 529603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:06:33+00:00 2026-05-13T09:06:33+00:00

I like to initialize 2-dimensional arrays as vector<vector<int> >(x,y) . x is passed to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:06:33+00:00Added an answer on May 13, 2026 at 9:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Those are type parameters for generic classes. May 13, 2026 at 3:24 pm
  • Editorial Team
    Editorial Team added an answer I had the same problem. I was missing the providerSelection… May 13, 2026 at 3:24 pm
  • Editorial Team
    Editorial Team added an answer Not a whole lot cleaner but this has the added… May 13, 2026 at 3:24 pm

Related Questions

I have a two dimensional array that I need to load data into. I
It seems strange that the language apparently includes no suitable functionality. I find myself
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
Im coming from a Unix world where I never had to develop something for
Hi I'm having some problem with 2D dynamic array. int main() { double **M;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.