Boost logo

Boost :

Subject: Re: [boost] [optional] Specializing optional to save space
From: Jeremy Maitin-Shepard (jeremy_at_[hidden])
Date: 2015-09-28 19:05:44


On Mon, Sep 28, 2015 at 2:50 PM, Sam Kellett <samkellett_at_[hidden]> wrote:

>
> surely this just isn't possible?
>
> before: i have an optional<int>, a, and i set it's value to -1. assert(a)
> passes.
> after: i have an optional<int>, b, which uses the optimized for space
> enhancement which internally uses the value -1 as it's sentinel value:
> assert(b) fails.
>
> this applies to any possible value of int -- so therefore somebody,
> somewhere will have a valid int that is an optional and some purely genetic
> code will get it wrong
>

Indeed, you wouldn't be able to define a space-saving specialization for
builtin types that have no invalid representations. You could, however,
define a custom type that wraps int and conveys the "not -1 semantics",
which might be a good thing to do anyway, and then specialize optional on
that. It would in most cases be possible (based on implementation details)
to define specializations for standard library types like string, vector,
etc., as David demonstrates.


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