Boost logo

Boost :

Subject: [boost] [static_if] Is there interest in a `static if` emulation library?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2014-08-31 21:37:23


Hello all,

With C++14 generic lambdas, it is possible to implement a sort of
`static if` (see also N3613):

template< typename T >
void assign ( T& x, T const& y ) {
    x = y;

    static_if<boost::has_equal_to<T>::value>(
        std::bind([] ( auto x, auto y ) {
            assert(x == y);
            std::cout << "asserted for: " << typeid(T).name() << std::endl;
        }, x, y)
    ).else_(
        [] ( ) {
            std::cout << "cannot assert for: " << typeid(T).name() << std::endl;
        }
    );
}

The bind around the generic lambda is a bit verbose... but this code
compiles, asserts when called on int, and does not assert when called
on x_t (tested on clang 3.4.2):

    int i = 1;
    struct x_t { } x;

    assign(i, i); // asserts
    assign(x, x); // does not assert

I have attached a reference implementation.

Thank you.
--Lorenzo

P.S. I didn't do much background research... sorry if this topic has
already been considered and discussed in Boost.




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