Boost logo

Boost :

Subject: Re: [boost] optional<optional<T>>
From: Andrei Alexandrescu (andrei_at_[hidden])
Date: 2009-09-03 12:08:10

Christopher Jefferson wrote:
> On 3 Sep 2009, at 03:43, Andrei Alexandrescu wrote:
>> Hello,
>> I'm defining an "optional" type for D's standard library modeled
>> similarly to Boost.optional. An interesting question came up - should
>> optional<optional<T>> fold itself into optional<T>, or is "double
>> optional" an interesting concept of its own?
>> I thought I'd ask here because by now there's a significant body of
>> experience with optional<T>. I perused the online documentation and
>> the forum and couldn't find information about that specific detail.
> Only a related note, I use a version of optional which takes the
> "m_initalized", effectively the boolean that tells you if a value is
> present, as a template parameter.
> This is because for many types, it is possible to know there is some
> value in the object itself which cannot take certain values, so can be
> overloaded.
> As an example, given a vector<T> implemented as:
> struct vector<T>
> {
> T* begin;
> T* end;
> size_t length
> };
> Then I know that (on my system) begin will never take (void*)(1), (it
> might be NULL), so I can reuse that to implement optional<T> without any
> extra space usage.
> The main problem with this, at least in C++, this is basically
> impossible to implement generically and legally, as you are effectively
> poking into an objects memory without knowing what it is, so I haven't
> tried to carefully formalise it.
> Chris

Yah, good point. The solution I had in mind for that was to also define
a two-parameter Optional: Optional<T, T nullValue>. Then if you want to
use it with e.g. getchar(), you'd use Optional<int, EOF>. Unfortunately,
in C++ this is not as general as it could because T is limited to a few
types (D does not have this particular restriction). Something I think
could work nicely for C++ would be to parameterize on an address, e.g.
Optional<T, const T* nullValuePtr>. Continuing on the getchar example,
this could work:

const int EOF_global = EOF;
typedef Optional<int, &EOF_global> GetcharResult;

In your case, you'd have to define a global vector object with the
pointers forged appropriately.


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