Boost logo

Boost :

Subject: [boost] [optional] How to define boost::none?
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2014-11-19 10:12:08

Hi Everyone.

I am puzzled with a tough development task. I wonder if anyone is able to
find a good solution.

I want to re-implement tag boost::none. The current implementation is using
a pointer to member which allows a harmful conversion from literal 0. Try

boost::optional<boost::rational<int>> orat = 0;
// this creates a disengaged optional.

I want to rewrite the definition with the following goals in mind:
1. boost::none_t is not implicitly convertible from literal 0;
2. The solution works in C++98
3. The solution is implementable in a header-only library. (defining some
member variable in a CPP file is not an option.

The only solution I was able to find so far is this:

struct none_t
  enum creator_t {creator};

  none_t none (none_t::creator);

It works, but I feel uncomfortable defining boost::none in every
translation unit. Any better ideas are welcome.


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