Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2012-02-10 03:10:10


> > int i = 1;
> > int j = 2;
> > optional<int&> oi = i;
> > optional<int&> oj = j;
> > i = j;

> > The effect here is that i remains 1, j remains 2 and oi and oj both
hold a
> > reference to j. You may find it less surprising but if you think of
> > optional reference as a regular reference that is initialized a bit
later,
> > the behavior is not what you would expect.
>
> Huh? How does oi come to hold a reference to j?

Apologies, I meant to say

 int i = 1;
 int j = 2;
 optional<int&> oi = i;
 optional<int&> oj = j;
 oi = oj;

Boost.Optional documentation explains it better:
http://www.boost.org/doc/libs/1_48_0/libs/optional/doc/html/boost_optional/rebinding_semantics_for_assignment_of_optional_references.html

Regards,
&rzej


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk