|
Boost : |
Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2012-02-14 13:48:35
+1 for optional<T&> with the current assignment semantics and optimization.
In my opinion, optional<T&> is better then T*, because:
- it is clear, that the object isn't owned (owning T* pointers should be
replaced by smart pointers, but that's another story);
- the default constructor of optional<T&> initializes it to none;
- it is clear, that the object actually is optional, whereas with T*, it
needs to be documented "can be null" or something;
- I don't have to write my own get_value_or function, no matter how trivial
it would be for pointers ;-) ;
- access is asserted, so logic errors are caught early (right?).
Also, I prefer optional<T&> to some dumb_ptr, because optional<T&> says
exactly what i mean, and dumb_ptr - not really.
A note about assignment semantics:
int i = 1, j = 2;
optional<int&> oi = i, oj = j;
oi = oj;
If many people find it confusing, that oi now contains a reference to j,
while i remains unchanged, I could live with this kind of assignment
disabled at compile-time, and replaced by some member function. But
personally I prefer to leave those semantics unchanged.
A note about optimization: as it has been said, it seems easier to just
provide the optimization, than discuss it over and over again.
Regards
Kris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk