Boost logo

Boost :

Subject: Re: [boost] An extension to boost::variant
From: Chandrashekhar Kumar (iitk.cs_at_[hidden])
Date: 2009-02-09 14:03:03

> >> ..My question is this: why can't boost::variant be clever about
> reference
> >> data types..

To quote from the documentation of boost::variant:

Every type specified as a template argument to
at minimum fulfill the above requirements. In addition, certain
features of variant are available only if its bounded types meet the
requirements of these following additional concepts:

   - Assignable<>:
   variant is itself *Assignable* if and only if every one of its bounded
   types meets the requirements of the concept. (Note that top-level
   types and reference types do *not* meet these requirements.)

So, boost::variant can not handle simple references by design(we will
discuss about the rational behind it soon once I have understood your
requirements more clearly).

Though if you insist on some way out with your intended usage sematics, then
let me think based on my understanding of your requirement so far.

> > An example will be better than a lengthy explanation,
> > please have a look at the test-cases in the boost distribution:
> > libs/variant/test/:
> > specially variant_reference_test.cpp
> >
> > IMHO, it will answer all your queries.

> Hmm... that doesn't seem to answer my question. boost::variant
> currently can not handle the following code:
> struct foo {};
> int main(int argc, const char** argv)
> {
> foo f;
> foo* pf = &f;
> boost::variant<foo*,foo&> v;
> v = f;

This is an error, because the declaration of variant above doesn't expect
type of foo.

So, probably you want to write:

    boost::variant < foo, foo *, foo & > v; //extra indentation for better
then, v = f;

Hold on, this is still not allowed :). I know this is what you want to know

So, you have to find a way to first get the job done then we may discuss the
rest of why!

Quoting from Boost.Ref :

"The Ref library is a small library that is useful for passing references to
function templates (algorithms) that would usually take copies of their

So, you would like to write instead :

boost::variant<foo, foo*, boost::reference_wrapper<foo> > v;

Now it is ok.

only then you can write:

    v = f; // now it is fine.

> printf("%d\n", &boost::get<foo&>(v) == &f);

The above code is again in error because you have not yet given "foo &" to
your variant.
Though it will be a run-time error(I mean variant will throw an exception
boost::bad_get which you need to catch and gracefully handle n exit from

So, having said this, let me guess again (why I have to do this again.. :-(
) what you want to write instead may be this:

printf("%d\n", &boost::get<foo>(v) == &f);

If you meant it, then I am more confused why you want to do this, knowing
that these 2 addresses can never be equal ??

> v = pf;
> printf("%d\n", boost::get<foo*>(v) == pf);
> pf = NULL;
> printf("%d\n", boost::get<foo*>(v) != pf)

you have missed a semicolon here :) (did you really try this code on your
machine before posting ? )

Now it looks like everything is fine as per the job in question.

> return 0;
> }
> My question is essentially this: why couldn't it?

Now why?

It is deceitfully simple design decision made by the author.

For now, I will like to quote from

originally part of the
Tuple <> library
by Jaakko Järvi. They were "promoted to boost:: status" by Peter Dimov
because they are generally useful. Douglas Gregor and Dave Abrahams
contributed is_reference_wrapper<>and

> Regards,
> Chris

HTH for now,


Boost list run by bdawes at, gregod at, cpdaniel at, john at