Boost logo

Boost :

Subject: Re: [boost] An extension to boost::variant
From: Chris Hamilton (chamilton_at_[hidden])
Date: 2009-02-09 15:06:21

> To quote from the documentation of boost::variant:
> Every type specified as a template argument to
> variant<>must
> 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
> const-qualified
> 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).

I'm quite aware of what the boost docs say, and 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,

>> 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.

No, but I want it to expect a type of *reference* to foo, in which case
the above line would be just fine.

> v = f; // now it is fine.

No, in fact this is not fine (did you try compiling your code?). If it
were, I'd have no problem using boost::reference_wrapper. The problem
remains: the variant<...> class doesn't know how to handle 'operator=(
foo& f )'.

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

Indeed... and it doesn't compile (even with the semi-colon, which was a
copy and paste error); that was the whole point of the exercise.


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