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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:40:39+00:00 2026-06-13T18:40:39+00:00

Consider the following: std::vector<std::unique_ptr<int>> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and

  • 0

Consider the following:

std::vector<std::unique_ptr<int>> ptrsToInts;
ptrsToInts.emplace_back(new int);

If reallocation occurs in the vector, and that fails (throwing std::bad_alloc), am I “safe” or will I leak an int?

C++11 23.3.6.5 [vector.modifiers]/1 says:

If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of T or by any InputIterator operation there are no effects.

which seems to indicate that this is a potential problem. That is, if there are “no effects”, then no unique_ptr ever was constructed, and therefore the destructor behavior one would rely on to delete that pointer would not occur. (Which might indicate that emplace_back should be banned for containers of unique_ptrs)

  • 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-06-13T18:40:40+00:00Added an answer on June 13, 2026 at 6:40 pm

    If reallocation is required and it fails, then yes, your object never went into the container and will thus be lost.

    However, it should be noted that this is pure user error. emplace_back should not be “banned” for containers of unique_ptr, because there are perfectly safe ways of doing this (such as reserveing the space beforehand, so you know it will always be there). Also, you can pass in whole unique_ptrs, since it’s perfectly capable of using a move constructor.

    So really, it’s your fault for not properly wrapping your non-RAII object (the int*) in a RAII object before the point where you could throw exceptions.

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

Sidebar

Related Questions

Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following code: typedef std::vector<int> cont_t; // Any container with RA-iterators typedef cont_t::const_iterator
Consider the following minimal example. #include <vector> class Data { std::vector<int>& v; public: Data(std::vector<int>&
Consider the following: std::vector<int> vec(1); // vector has one element std::fill(vec.begin(), vec.begin(), 42); std::fill(vec.begin()+1,
Consider the following code snippet: #include <vector> using namespace std; void sub(vector<int>& vec) {
The Problem Consider the following vectors: std::vector<std::string> extensions; extensions.push_back(.cpp); extensions.push_back(.CPP); extensions.push_back(.h); extensions.push_back(.H); std::vector<std::string> caselessUniqueExtensions;
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider the following piece of code: 1 typedef std::deque<int> mydeque_t; 2 mydeque_t mydeque; 3
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int

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.