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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:56:07+00:00 2026-05-19T04:56:07+00:00

I have an optional POD struct that will be contained inside a union. boost::optional<>

  • 0

I have an optional POD struct that will be contained inside a union.
boost::optional<> holds its type by value, so I thought this could work:

union helper
{
    int foo;
    struct 
    {
        char basic_info;
        struct details {
            //...
        };

        boost::optional<details> extended_info;
    } bar;
    //  ...
};

helper x = make_bar();

if( x.bar.extended_info )
{
    // use x.bar.extended_info->elements
}

but VS2008 complained that my bar struct now had a copy constructor due to the boost::optional<details> element.

As a replacement, I’ve added a boolean flag to indicate whether the optional parameter is valid, but it’s clunky:

union helper
{
    int foo;
    struct 
    {
        char basic;
        struct details {
            bool valid;
            //...
        } extended;
    } bar;
    //  ...
};

I considered implementing details::operator bool() to return the details::valid variable, but that’s obscure and a disservice to humanity.
boost::optional<> clearly documents the syntax and intent and doesn’t require detective work.

Finally, the helper union needs to be POD, so I can’t do any dynamic allocation – otherwise I would use a pointer.

Any suggestions for something syntactically similar to boost::optional<> that’s usable in a union?

  • 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-19T04:56:07+00:00Added an answer on May 19, 2026 at 4:56 am

    As others have mentioned, the ideal thing to do is to change from a union to a boost::variant<>.

    However, if this isn’t possible, you can implement a POD approximation of boost::optional<> as follows:

    Implementation

    template <typename T>
    class Optional
    {
        T value;
        bool valid;
    
    public:
    
        // for the if(var) test
        operator bool() const  {  return valid;  }
    
        //  for assigning a value
        Optional<T> &operator=(T rhs)   
        {  
            value = rhs;  
            valid = true;  
            return *this;  
        }
    
        //  for assigning "empty"
        Optional<T> &operator=(void *)  
        {  
            valid = false;  
            return *this;  
        }
    
        // non-const accessors
        T &operator*()   {  return  value;  }
        T *operator->()  {  return &value;  }
    
        // const accessors
        const T &operator*()  const  {  return  value;  }
        const T *operator->() const  {  return &value;  }
    };
    

    The const accessors are necessary if you are holding a const instance of Optional<>.

    Usage

    Like a pointer, Optional<T> has no default state and must be initialized before you can rely on it (null or not).
    Unlike boost::optional<T>, Optional<T> cannot be constructed from its T value type, and can only be constructed from another Optional<T>.
    If you really want to value- or null-initialize it at construction, you could make a helper class with an operator Optional<T>(). I chose not to.

    Construction

    Optional<details> additional_info;
    Optional<details> more_info(additional_info);
    

    Assignment

    // if there's no additional info
    additional_info = 0;
    
    // if there is extended info
    details x;
    //  ...populate x...
    additional_info = x;
    

    Data access

    if( extended_info )
    {
        extended_info->member;
        // - or -
        details &info = *extended_info;
    }
    

    So – it didn’t turn out to be too bad. It doesn’t make me feel quite warm and fuzzy, but it gets the job done.

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

Sidebar

Related Questions

I have a database that hold's a user's optional profile. In the profile I
I have an asp.net text form that contains numerous decimal fields that are optional.
I have a base class with an optional virtual function class Base { virtual
I have an application which had optional extras depending on if the user has
We have a question with regards to XML-sig and need detail about the optional
I have a form made up of multiple, optional subparts - each of which
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as 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.