Boost logo

Boost :

Subject: Re: [boost] [optional] How to define boost::none?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2014-11-19 10:26:53


On Wed, Nov 19, 2014 at 6:12 PM, Andrzej Krzemienski <akrzemi1_at_[hidden]> wrote:
> 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.

  struct none_t {};
  const none_t none = none_t();

Technically, this is dynamic initialization, but for our purpose it
doesn't matter (there are no data members in none_t).

Since none has internal linkage, it is translation unit-specific.
Technically, this can cause ODR violations in some cases. To avoid
that you can declare it like this:

  struct none_t {};

  template< typename T >
  struct singleton
  {
    static const T instance;
  };
  template< typename T >
  const T singleton< T >::instance = T();

  const none_t& none = singleton< none_t >::instance;


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