Boost logo

Boost :

Subject: Re: [boost] [optional] How to define boost::none?
From: David Stone (david_at_[hidden])
Date: 2014-11-20 09:54:40

Is it really such a problem to have this object in every translation unit?
Is it beyond current linker technology to merge these identical copies,
thus preventing binary bloat?

If it is, you could use something like the following solution:

template<typename T>
class optional {
    optional(none_t) { /* whatever */ }
    enum dummy{};
    optional(dummy *);

​This causes users attempting to initialize optional with a literal 0 or
nullptr to receive an error about ambiguous overloading. However, it still
allows it to be used as:

optional<int> oi(0);

if you also have a public constructor that accepts a T const & (which you
do). The only tricky part is code like the following:

optional<int *> op0(0);
optional<int *> opn(nullptr);

Both of those give ambiguous overloads. The first, from integer literal 0,
already does not work, because the template parameter in your current
constructor is deduced as int, and there is no implicit conversion from int
to int *. C++03 users would have to say something like static_cast<int
*>(0). The second, however, currently compiles, and we probably want to
preserve this behavior. Note that if we had said optional<int *> opn =
nullptr, it would fail. We probably want that to work just like explicit

This means we need to selectively enable and disable some of these
constructors under certain conditions. I believe the following will work
for your needs:

// Same none_t and none definition as in boost now, just so people can see
it without looking it up
namespace detail { struct none_helper{}; }

typedef int detail::none_helper::*none_t ;

none_t const none = (static_cast<none_t>(0)) ;

template<typename T>
class optional {
    template<typename U>
    optional(U, typename std::enable_if<std::is_same<U, none_t>::value,
int>::type * = 0): m_b(false) {}

    optional(T): m_b(true) {}

    operator bool() const { return m_b; }
    bool m_b;
    enum dummy{};
    template<typename U>
    optional(U, typename std::enable_if<std::is_same<U, dummy *>::value,
int>::type * = 0);

I used C++11 type traits because I can never remember when I need to use
the _c versions of boost type traits, but it should be fairly
straightforward to translate that for someone more familiar with the boost

The following code compiles and runs. Commented out lines do not compile.

class C {};

int main() {
    boost::optional<int> bi(0);
    boost::optional<int> bia = 0;

    // boost::optional<C> bc(0);
    boost::optional<C> bca = 0;

    boost::optional<int *> bpn(nullptr);
    // boost::optional<int *> bpna = nullptr;
    boost::optional<int *> bp0c(static_cast<int *>(0));
    boost::optional<int *> bp0ca = static_cast<int *>(0);
    // boost::optional<int *> bp0(0);
    // boost::optional<int *> bpoa = 0;

    optional<int> ni(0);
    optional<int> nia = 0;

    // optional<C> nc(0);
    // optional<C> nca = 0;
    optional<C> ncu(none);
    optional<C> ncua = none;

    optional<int *> npn(nullptr);
    optional<int *> npna = nullptr;
    optional<int *> np0c(static_cast<int *>(0));
    optional<int *> np0ca = static_cast<int *>(0);
    optional<int *> np0(0);
    optional<int *> np0a = 0;
    optional<int *> npu(none);
    optional<int *> npua = none;

Are all of these test cases the correct result?

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