Boost logo

Boost :

Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2014-09-01 21:27:51


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.

    void f ( ) {
        value_type w;

        static_if<B>(
            std::bind([ ] ( auto w, auto v ) {
                w = v;
            }, std::ref(w), std::ref(v))
        );
    }
};

int main ( ) {
    x<true> x1;
    x1.f();
    x<false> x0;
    x0.f();
    return 0;
}

--Lorenzo


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