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

  • SEARCH
  • Home
  • 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 6648647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:40:48+00:00 2026-05-26T00:40:48+00:00

Take the following code: std::vector<std::vector<int>> v(10, 10); This code doesn’t compile with libstdc++. It

  • 0

Take the following code:

std::vector<std::vector<int>> v(10, 10);

This code doesn’t compile with libstdc++. It does compile with Visual Studio’s C++ library, however. The behavior I would expect is that v is filled with 10 vectors of size 10, and this is what I get with Visual Studio.

The constructor invoked with Visual Studio is the one taking two iterators. The constructor itself is defined as:

template<class _Iter>
vector(_Iter _First, _Iter _Last)
    : _Mybase()
{   // construct from [_First, _Last)
    _Construct(_First, _Last, _Iter_cat(_First));
}

There are two versions of the template function _Construct. Both have the same signature, but one initializes the vector from a range, and the other initializes the vector with N copies of the value type copy constructed from the second parameter. In this case, the template parameters are only valid for the second version of _Construct.

The result is that v is filled with 10 copies of a vector that was copy constructed from the value 10. The same code path is taken by constructing it like this, as you would have to do for the same effect using libstdc++:

std::vector<int> temp(10);
std::vector<std::vector<int>> v(10, temp);

Which implementation is correct here? Is this a libstdc++ bug or an extension of Visual Studio’s C++ library?

Edit: Just to clarify, I’m not asking if it should be invoking the range constructor. I’m asking which C++ implementation has the correct behavior, regardless of which path it takes to achieve it.

  • 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-26T00:40:49+00:00Added an answer on May 26, 2026 at 12:40 am
    std::vector<std::vector<int>> v(10, 10);
    

    This should not compile, because the vector’s single argument constructor is explicit.

    C++03:

    explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());
    

    C++11:

    explicit vector(size_type n);
    

    This means a number like 10 can’t implicitly turn into a vector of size 10.

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

Sidebar

Related Questions

Take note of the following C++ code: #include <iostream> using std::cout; int foo (const
Take the following two lines of code: for (int i = 0; i <
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else
I'm trying to write a piece of code that will do the following: Take
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
Take the following class as an example: class Sometype { int someValue; public Sometype(int
Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm>
I am confused about the interface of std::find . Why doesn't it take a
Previous, I have the following code. double* a[100]; for (int i = 0; i

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.