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
this:

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:

[code]
struct none_t
{
  enum creator_t {creator};
  none_t(creator_t){}
};

namespace
{
  none_t none (none_t::creator);
}
[/code]

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

Regards,
&rzej


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