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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:41:42+00:00 2026-06-10T18:41:42+00:00

I am trying to write very simple Any object, which can hold object of

  • 0

I am trying to write very simple Any object, which can hold object of any type. I want it use inside container, to achieve heterogeneous container.

#include <iostream>
#include <vector>
#include <string>

struct Any
{
    template < typename T >
    Any(const T & t) 
        :p(new storageImpl<T>(t)) { }

    ~Any() 
    {
        delete p;
    }

    struct storage
    {
        virtual ~storage() {}
    };

    template <typename T>
    struct storageImpl : storage
    {
        storageImpl(const T & t) : data(t) {}
        T data;
    };

    template <typename T>
    T & get()
    {
        storageImpl<T> * i = static_cast<storageImpl<T>*>(p);
        return i->data;
    }

    storage * p;
};

usage

int main ()
{
    //block1
    Any copy(Any(std::string("foo")));      
    std::cout << copy.get<std::string>();   

    //block2
    std::vector<Any> cont;
    cont.push_back(Any(5));
    cont.push_back(Any(37.9f));
    std::cout << cont[0].get<int>();    
    std::cout << cont[1].get<float>();  
}

I have problem with copy-construction.

When I push Any into vector (//block2), the unnamed Any gets destroyed, so pointer gets deleted, and pushed object is no longer valid.

So I have 2 questions:

1, How to write copy constructor for class Any?

2, Why isn’t unnamed Any in block1 destroyed, so its pointer isn’t deleted?\

EDIT
I have tried

template <typename T>
Any(const Any & rhs)
    :p(new storageImpl<T>(rhs.get()))
{
}

but it doesn’t get triggered.

  • 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-10T18:41:43+00:00Added an answer on June 10, 2026 at 6:41 pm

    Any is not a template class. Trying to template the copy-constructor as template <typename T> Any(const Any & rhs) is meaningless.

    What you could do is to use the virtual constructor idiom, to let the storageImpl copy itself. This is also the method used in Boost.Any.

    struct Any {
        template < typename T >
        Any(const T& t) : p(new storageImpl<T>(t)) {}
    
        Any(const Any& other) : p(other.p->clone()) {}
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
        ~Any() { delete p; }
    
        struct storage {
            virtual ~storage() {}
            virtual storage* clone() = 0;
         // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        };
    
        template <typename T>
        struct storageImpl : storage {
            storageImpl(const T & t) : data(t) {}
    
            virtual storage* clone() { return new storageImpl(data); }
         // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
            T data;
        };
    
        template <typename T>
        T& get() {
            storageImpl<T>* i = static_cast<storageImpl<T>*>(p);
            return i->data;
        }
    
        storage * p;
    };
    

    Notice that this implementation have a lot of problems, e.g. the get() method won’t check whether the Any is really holding a T. It is still better to use a well-tested library e.g. Boost.Any.


    Why isn’t unnamed Any in block1 destroyed, so its pointer isn’t deleted?

    Copy elision.

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

Sidebar

Related Questions

I am trying to write a very simple RSS channel which would display the
I'm trying to write a very simple program, I want to print out the
I am currently trying to write a very simple app that sends an object
I'm trying to write a very simple regular expression that matches any file name
I'm trying to write a very simple Python utility for personal use that counts
i am trying to run a very simple java program. i want to write
I am trying to write a very simple web-based email client from scratch with
I'm trying to write a very simple image processing program for fun and practice.
I am trying to write a very simple method to remove duplicates in a
For fun I'm trying to write a very simple server in C. When I

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.