Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2012-02-16 10:38:06

Luke wrote:
>>This doesn't convince me. If the pointer can't be zero (and if somebody else owns it), there is no good reason not to use a reference. Well, if the pointer is a class member, using a reference might be impossible.

>An std container of references does not compile, a std container of pointers does. In code with dynamic polymorphic type systems pointers get used a lot, and often more than they should.

Yeah, those containers want your type to be default constructable which ref is not. If you use one of those containers you should use a pointer and from the point of view of that container it really can be null. If you know it won't be convert back to ref, but be careful:
        std::map<int,entry_type*> m;
        m[3]; // constructs a null pointer

Changing the code to:
        std::map<int,optional<entry_type&> > m;
won't make it any safer or easier to read. If fact the ref syntax on assignment might make it worse:
        m[3]=x; //quitely takes address of x and stores it

Normal refs have to be assigned were they're declared or in initializer lists. This kind of a ref container seems sneaky to me.

The intrusive conainers work by ref though and it makes sense since the elements can not be null.
Note unlink_leftmost_without_rebalance() returns pointer, because it could be null.


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