|
Boost : |
Subject: Re: [boost] [optional] How to define boost::none?
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2014-11-19 10:31:57
2014-11-19 16:28 GMT+01:00 Krzysztof Czainski <1czajnik_at_[hidden]>:
> 2014-11-19 16:12 GMT+01:00 Andrzej Krzemienski <akrzemi1_at_[hidden]>:
>
> > 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
> >
>
> Hi Andrzej,
>
> Why do you need the creator_t? What's wrong with a user creating other
> instances of none_t?
>
> Did you forget to make none const?
>
> And then, why wouldn't a straightforward solution work:
>
> struct none_t {};
>
> none_t const none = {}
>
I think it validates ODR, or doesn't it?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk