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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:28:28+00:00 2026-06-14T23:28:28+00:00

I have read that one must not store std::auto_ptr in std::vector and that boost::ptr_vector

  • 0

I have read that one must not store std::auto_ptr in std::vector and that boost::ptr_vector could be used instead. I have been able to do so, I don’t know however how to use ptr_vector, when I don’t want to store pointers, but a struct, which has a pointer member.

In this example, I want to open some files and store the associated ofstream object with some additional data, for later use. I would like to replace the file field of struct data with a smart pointer. Since the vector<data> v should be the owner, I think that a shared_ptr would work, but wouldn’t be appropriate.

What should I replace the naked pointer file with?

#include <iostream>
#include <fstream>
#include <vector>

struct data {
  std::string filename;
  std::ofstream* file;

  data(const std::string filename, std::ofstream* file)
    : filename(filename), file(file)
  {
  }
};

std::vector<data> open_files()
{
  std::vector<data> v;
  v.push_back(data("foo", new std::ofstream("foo")));
  return v;
}

int main()
{
  std::vector<data> v = open_files();

  /* use the files */
  *(v[0].file) << "foo";

  delete v[0].file;  // either rely on dtor to close(), or call it manually
}

Update:
I feel I have done a sub optimal job in describing my problem, let me try with another example. Also I am looking for a C++03 solution:

#include <memory>
#include <vector>
#include <boost/ptr_container/ptr_vector.hpp>

struct T {
  std::auto_ptr<int> a;
};

int main()
{
  // instead of
  // std::vector<std::auto_ptr<int> > v;
  // use
  boost::ptr_vector<int> v;

  // what to use instead of
  // std::vector<T> w;
}
  • 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-14T23:28:30+00:00Added an answer on June 14, 2026 at 11:28 pm

    Concerning your data class, I would suggest using an std::unique_ptr<std::ofstream>. This is not to save you from an accidental memory leak, since you are deleting the pointer in the constructor, but rather to make the ownership explicit. A user of your code would have to know what data is doing with the pointer it takes in the constructor:

    std::ofstream ofs;
    {
       data d1("crash", &ofs);
    } // error! d1 will attempt to delete stack allocated object
    
    std::ofstream* pOfs = new std::ofstream(....);
    data d2("crash again", pOfs);
    delete pOFs; // user thinks data makes a deep copy
    

    However, with unique_ptr the intend is clear, hence it is harder to make mistakes:

    data d3("OK", std::unique_ptr<std::ofstream>(new std::ofstream(....)));
    
    std::unique_ptr<std::ofstream> pOfs2(new std::ofstream(....));
    data d4("OK", pOfs2); // safe, pOfs's contents have been safely moved
    
    // we can check pOfs2 after the move
    if (pOfs2) { /*  */ } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with WCF .NET 3.5 SP1 and have read that one does
i have read in more then one place that using NHibernate's Identifier as Primary
I have read that GLSL (specifically v1.0.17: my application is running under WebGL) compilers
I have read that in Java interfaces can't be instantiated ( in the documentation,
I have read that you should keep the number of connections in your database
I have read that for enabling the auto return I have to enable this
I have read that you can do it, but would this really improve performance
I have read that gwt-ext is slow and it seems too bulky. How does
I have read that 'Normal' ARM instructions are fixed length - 32 bits. And
I have read that LinkedHashMap has faster iteration speed than HashMap because its elements

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.