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?
As others have mentioned, the ideal thing to do is to change from a
union
to aboost::variant<>
.However, if this isn't possible, you can implement a POD approximation of
boost::optional<>
as follows:Implementation
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 itsT
value type, and can only be constructed from anotherOptional<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
Assignment
Data access
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.