On Saturday, 25 August 2018 21:08:30 UTC+3,
woodb...@gmail.com wrote:
> On Friday, August 24, 2018 at 1:31:03 AM UTC-5, Öö Tiib wrote:
> > On Friday, 24 August 2018 04:14:20 UTC+3, Lynn McGuire wrote:
> > > “Modern C++” != “New(est) Standard”
> > >
https://arne-mertz.de/2018/08/modern-c-newest-standard/
> > >
> > > After writing code in Smalltalk for several years, I love strong typing.
> >
> > He forgets to mention loose, minimally intrusive coupling.
> > Thanks to lambdas we do not need type unsafe void(*)(void*)
> > and void* pair for that.
> >
> > Most modern from C++17 are variant and optional. Simply awesome
> > and ridiculously useful. The union was always unsafe and usage of
> > raw or unique_ptr to represent little, possibly missing value was
> > so sadly inefficient.
>
> I think variant is kind of useful, but am not as convinced
> about optional. I've considered several times using a
> vector of optional rather than a vector of unique_ptr here:
>
https://github.com/Ebenezer-group/onwards/blob/master/src/cmw/tiers/cmwA.cc
> , but optional:
> increases the size of my text segment by about 2% and
> results in more expensive moves than unique_ptr.
I can't imagine how you measured? Optional means additional bool,
when unique_ptr means additional pointer, additional indirection
and additional dynamic allocation/deallocation. The situations may
certainly vary but can't be that by that lot.
> The benefit of optional would be to avoid the heap allocation
> and deallocation. I avoid the heap in general, but in this case
> think the advantages of unique_ptr are worth it, especially if
> the size of my type grows over time leading to even bigger text
> segments and more expensive moves.
>
> I'm not a big proponent of unique_ptr, but in this case I find
> it helpful.
The unique_ptr is useful when the component is polymorphic. Optional
can't be polymorphic. It is also easy to see how unique_ptr can
provide storage performance advantage when the component is often
missing (rarely present) but how it can provide better speed
performance? Can you provide a cite or benchmark?
In general what is currently pricey is not memory amount, nor throughput
but it is latency. Optionals are local and that always means less cache
misses and better latency.