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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:06:23+00:00 2026-06-10T06:06:23+00:00

I came across an older pondering Type erasure techniques , by Xeo , and

  • 0

I came across an older pondering Type erasure techniques, by Xeo, and I became to wonder, how should one to amend that code to make it work with std::unique_ptr'ers and std::shared_ptr'ers.

The code in the post can be found here. The code won’t compile if fed with something containing unique_ptrs and the data in shared_ptr'ers become garbage. What I tried was a class inherited from a templated base class, so maybe it was somewhat complicated too. Now, this is mainly out of curiosity, as I became to wonder if it would be difficult (in general case) as this could become handy when storing complex objects, say, in std::vector when Boost.Any isn’t available for use.

Edit: I noticed I just had a bug in my code whilst testing, the code works just fine with shared_ptr'ers (the contents aren’t garbage), though not with unique_ptr'ers. And then also, why not store a newed instance of this type erasured Any_Virtual (as in code provided by Xeo) to, say, std::unique_ptr'ers.

I guess then the questions would be:

  1. How to amend the Any_Virtual so that it could work with std::unique_ptr?
  2. Which one would be better design, a std::vector<Any_Virtual> objects, where Any_Virtual holds a smart pointer, or a std::vector<std::unique_ptr<Any_Virtual>> objects? Or does it even matter?
  • 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-10T06:06:24+00:00Added an answer on June 10, 2026 at 6:06 am

    When you use type-erasure there is always a set of requirements that a type must satisfy to be compatible with the type-erasing container: this set is called the model. In particular, the holder<T>::clone member that copies *this requires in turn that held_ (of type T, the type being erased) be copy constructible. Hence the model of your type-erasing class is copy constructible.

    However a type containing an std::unique_ptr will not be copy constructible out of the box.

    There is no obvious fix without knowing what you want to achieve. Perhaps you really want the model to be less strict, e.g. it could only require to be move constructible (which a type containing an std::unique_ptr can easily fulfill, right out of the box). Or perhaps you really want those types that do hold std::unique_ptrs to be copy constructible.

    In my opinion the very worst you can do with type-erasure is to compromise, and make an operation on the model work conditionally, depending on whether the type being erased supports such an operation itself. Here that would mean that coyping an Any_Virtual value would result in an exception if it happened to hold a value of non-copy constructible erased type.

    Perhaps more worryingly, the fact that you obtain garbage std::shared_ptrs heavily suggest that there is a problem with either your implementation of Any_Virtual or your use of it. You should definitively not assume that there is a problem with using std::shared_ptr in tandem with Any_Virtual alone, but that there might be a problem with using anything with Any_Virtual. Since I haven’t noticed a problem in the implementation (but I could easily have overlooked something), I’d like to see an example of a program exhibiting the problem.

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

Sidebar

Related Questions

I came across a controller in an older set of code (Rails 1.2.3) that
I came across this code http://support.microsoft.com/kb/320348 which made me wonder what would be the
I just came across a line of code that was written by a co-worker
So I was going through some older code at work and came across this:
I recently came across a problem with my code that was caused by certain
I came across some legacy code that is supposed to keep an incrementing sequence
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came across this Linq to Sql code in an application I am maintaining:
I came across a piece of code which looks like this: jQuery(function($) { $('#saySomething').click(function()
I came across this due to a bug in my code and I'm curious

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.