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 7539249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:19:57+00:00 2026-05-30T07:19:57+00:00

In C++11, std::vector has the constructor vector(size_type n) which will default construct n items

  • 0

In C++11, std::vector has the constructor vector(size_type n) which will default construct n items in place, which can be used with default constructible, movable, non-copyable classes.

However, unlike every other vector constructor, there is no variant that takes an allocator, and I have resorted to the following:

// Foo is default constructible and moveable, but not copyable
const int n = 10; // Want 10 default constructed Foos
std::vector<Foo, CustomAllocator> foos(allocator);
foos.reserve(n);
for (int i = 0; i < n; ++i)
   foos.emplace_back();

Is there a better way to accomplish this? Is there a specific reason vector(size_type n, const Allocator& alloc) was omitted from the standard?

  • 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-30T07:19:58+00:00Added an answer on May 30, 2026 at 7:19 am

    After thinking about it, it might not be a defect after all.

    It is possible that allocator_type and value_type are perversely the same type. In that case, which function would vector(3, alloc) call? The constructor that takes a default value to copy-initialize into all of the elements, or the one that takes a size and an allocator? That’s ambiguous, and thus a compile error.

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

Sidebar

Related Questions

I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has
I'm trying to design a class which has two vectors of large sequences. std::vector<double>
C++ has std::vector and Java has ArrayList , and many other languages have their
I have a std::vector containing a handful of numbers, which are not in any
I've a class Foo<T> which has a vector of smart pointers to Shape derived
I saw the following code used to delete one selected element from std::vector :
std::vector<Foo> vec; Foo foo(...); assert(vec.size() == 0); vec.reserve(100); // I've reserved 100 elems vec[50]
If std::vector and friends are self resizing, does that mean if I declare a
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
If I have a std::vector or std::map variable, and I want to see the

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.