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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:05:12+00:00 2026-05-18T00:05:12+00:00

I want a safe C++ pointer container similar to boost’s scoped_ptr , but with

  • 0

I want a safe C++ pointer container similar to boost’s scoped_ptr, but with value-like copy semantics. I intend to use this for a very-rarely used element of very-heavily used class in the innermost loop of an application to gain better memory locality. In other words, I don’t care about performance of this class so long as its “in-line” memory load is small.

I’ve started out with the following, but I’m not that adept at this; is the following safe? Am I reinventing the wheel and if so, where should I look?

template <typename T> 
class copy_ptr {
    T* item;
public:
    explicit copy_ptr() : item(0) {}
    explicit copy_ptr(T const& existingItem) : item(new T(existingItem)) {}
    copy_ptr(copy_ptr<T> const & other) : item(new T(*other.item)) {}
    ~copy_ptr()  { delete item;item=0;}

    T  * get() const {return item;}
    T & operator*() const {return *item;}
    T * operator->() const {return item;}
};

Edit: yes, it’s intentional that this behaves pretty much exactly like a normal value. Profiling shows that the algorithm is otherwise fairly efficient but is sometimes hampered by cache misses. As such, I’m trying to reduce the size of the object by extracting large blobs that are currently included by value but aren’t actually used in the innermost loops. I’d prefer to do that without semantic changes – a simple template wrapper would be ideal.

  • 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-18T00:05:13+00:00Added an answer on May 18, 2026 at 12:05 am

    No it is not.

    You have forgotten the Assignment Operator.

    You can choose to either forbid assignment (strange when copying is allowed) by declaring the Assignment Operator private (and not implementing it), or you can implement it thus:

    copy_ptr& operator=(copy_ptr const& rhs)
    {
      using std::swap;
    
      copy_ptr tmp(rhs);
      swap(this->item, tmp.item);
      return *this;
    }
    

    You have also forgotten in the copy constructor that other.item may be null (as a consequence of the default constructor), pick up your alternative:

    // 1. Remove the default constructor
    
    // 2. Implement the default constructor as
    copy_ptr(): item(new T()) {}
    
    // 3. Implement the copy constructor as
    copy_ptr(copy_ptr const& rhs): item(other.item ? new T(*other.item) : 0) {}
    

    For value-like behavior I would prefer 2, since a value cannot be null. If you go for allowing nullity, introduces assert(item); in both operator-> and operator* to ensure correctness (in debug mode) or throw an exception (whatever you prefer).

    Finally the item = 0 in the destructor is useless: you cannot use the object once it’s been destroyed anyway without invoking undefined behavior…

    There’s also Roger Pate’s remark about const-ness propagation to be more “value-like” but it’s more a matter of semantics than correctness.

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

Sidebar

Related Questions

I want to reference a COM DLL in a .NET project, but I also
I want to log onto Stack Overflow using OpenID, but I thought I'd set
I am building a DLL that another application would use. I want to store
In my attempt to develope a thread-safe C++ weak pointer template class, I need
Is it safe to use longjmp and setjmp in C++ on linux/gcc with regards
I'd want to hear various opinions how to safely use c++ in mission critical
I want to create a Thread safe JSP page. It is possible in Servlet
I want to perform animation on main thread (cause UIKit objects are not thread-safe),
I want to perform animation on main thread (cause UIKit objects are not thread-safe),
I want to read and write python-source-files from the file system in a thread-safe

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.