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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:52:11+00:00 2026-05-14T02:52:11+00:00

I have a universal data type, which is passed by value, but does not

  • 0

I have a universal data type, which is passed by value, but does not maintain the type information. We store only pointers and basic data types(like int, float etc) inside this. Now for the first time, we need to store std::string inside this. So we decided to convert it into std::string* and store it. Then comes the problem of destruction. We don’t like to copy the std::string every time. So i am thinking of an approach like this. Say the data type looks like this

class Atom
{
public :
     enum flags
     {
         IS_STRING,
         IS_EMPTY,
         HAS_GOT_COPIED,

         MARKER
     };

private:
     void*   m_value;
     std::bitset<MARKER>   m_flags;

public:
    .....
    Atom( Atom& atm )
    {
        atm.m_flags.set( HAS_GOT_COPIED );
        ..... 
    }
    .....
    ~Atom()
    {
        if( m_flags.test(IS_STRING) && !m_flags.test(HAS_GOT_COPIED) )
        {
             std::string*  val = static_cast<std::string*>(m_value);
             delete val;
        }
    }
};

Is this a good approach to find out whether there is no more reference to std::string*? Any comments..

I have looked at boost::any and poco::DynamicAny. Since i need serialization, i can’t use those.

Thanks,
Gokul.

  • 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-14T02:52:11+00:00Added an answer on May 14, 2026 at 2:52 am

    One major flaw with this approach is that you really need a reference count, not a single bit “has been copied” flag. The bit won’t work if you copy the string multiple times. As written, you will get into trouble if you create a copy of an Atom and delete the copy before the original:

    Atom a("hello world");
    
    if (...) {
        Atom b(a);
        // b is destroyed, deleting the string
    }
    
    // Uh oh, the string's been deleted but a is still referencing it.
    cout << (string) a;
    

    I would not re-invent the wheel. If boost::any doesn’t work out of the box you could still use it internally in place of your m_value field to store the data for your Atom class. That would take care of all the construction/copying/destruction details for you.

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

Sidebar

Related Questions

I have a task: investigate possibility not to install universal forwarder and use only
I have a universal app which uses simple pLists for data storage and would
I have a universal view controller class which all of the view controller classes
I have a Core Data app that will end up being an iPhone/iPad universal
I have a Excel Spreadsheet with lab data which looks like this: µg/L (ppb)
I have the below script. It is not rendering the 2006 data. Is there
I have recently started a Universal App project. Does the Universal app automatically filter
Say you have a bunch of files. Say you can store meta data to
I have wrote universal script to get all rows from Doctrine Table Models, but
I have an Xcode Universal Storyboard project that is properly displaying data for the

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.