Boost logo

Boost :

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

> what I'm proposing is an extension to the variant class such that
> references *can* be handled
> (and have it satisfy the Assignable and default constructible concepts,
> at least as far as the internals of the variant object are concerned, by
> internally storing a pointer).
> Caveat: There would be a small violation to the "Never Empty Guarantee"
> if a reference was the first templated type and the variant was default
> constructed; in this case, it would be default constructible (as
> internally it would be stored using a pointer), but the pointer would
> have a null value. Trying to use this value would cause an error,
> obviously.

So you are consciously asking for something which you will know will pose a
threat to the existing promises laid by the present variant. Interesting!


> The problem remains: the variant<...> class doesn't know how to handle
> 'operator=(
> foo& f )'.

It is not a problem I think rather it is feature in the light the
caveat(just one of many such worms) you mentioned above.

So by now, you have learned that you can gladly use variant like
boost::variant<foo*,boost::reference_wrapper<foo> > v.

Now to the question of "The problem is that the user has to be aware that
they are passing a
reference by explicitly wrapping it.":

To achieve this, you can easily cook up some wrapper which will do the type
forwarding magic by using type traits gimicks like is_reference on the user
type before passing it to variant.

So, now you will like to say : it is good that boost::variant
doesn't know how to handle an '= instance_of_foo', and realize this
means : you have to read and understand other boost libs too(for writing
metaprograms based wrapper) to write a skeleton wrapper to help your user.

HTH and you have somewhat better understanding of boost libs and its

If you are looking for nice pdf documentation, here you go:

Enjoy reading before posting!

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