Boost logo

Boost :

Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2014-09-20 16:22:19


On 2014-09-01 21:27, Lorenzo Caminiti wrote:
> On Mon, Sep 1, 2014 at 3:42 PM, Sebastian Redl
> <sebastian.redl_at_[hidden]> wrote:
>> Either way, my need for static if is to conditionally have specific declarations in a class template or not, so unless the library can do this (the suggested usage won’t work in a pure declaration context), it’s no use to me.
>
> I know... too bad enable_if cannot be used to disable these data
> member declarations.
>
> The closest I ever came to this is to keep the data member
> declarations but make them "unusable". Not quite the same, but then
> you can use static if to program statements only when the data members
> are actually usable so they compile. For example:
>
> #include "static_if.hpp"
> #include <boost/mpl/if.hpp>
> #include <boost/noncopyable.hpp>
> #include <functional>
>
> class unusable : boost::noncopyable {};
>
> template< bool B >
> struct x {
> typedef typename boost::mpl::if_c<B, int, unusable>::type value_type;
> value_type v; // unfortunately, this can't actually be disable...
> so make it useless.
<snip>

If we wanted to support this use case with a simpler syntax than
inheritance then I think the smallest language change would be to permit
data members of type void. Any use of such members would be ill-formed,
and they would not be counted amongst the data members for the purposes
of e.g. is_empty, is_standard_layout, etc.

Then you could write the above code, with 'unusable' replaced by 'void'.

If this misuse of void is too abhorrent, then a new keyword would of
course be an option.

Just a thought... someone keen could try to push this on std-proposals :).

John Bytheway


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