Boost logo

Boost :

Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: Robert Ramey (ramey_at_[hidden])
Date: 2014-09-03 01:19:14


here is the original example:

template< typename T >
void assign ( T& x, T const& y ) {
    x = y;
    if(boost::has_equal_to<T>::value){
            assert(x == y);
            std::cout << "asserted for: " << typeid(T).name() << std::endl;
    }
    else {
            std::cout << "cannot assert for: " << typeid(T).name() <<
std::endl;
    }
}

depending on the type of T, only one of the branches need be compiled.
Whatever is done with the other branch will be thrown away so what's the
point of compiling it. Of course the the C++ parser has to determine where
the branches start an end, but that's way, way short of doing full
compilation of the unused branch.

It's been pointed out that the unused branch needs to be compiled even if
the result is to be thrown away so that the program won't contain invalid
code which will only create a surprise when another type for T is used.
Actually, I'm quite sympathetic to this argument. Generic code shouldn't
resolve to invalid code for some types. That mean that it's not really
generic.

But I should say that I'm not sure that the original example illustrates the
utility of the proposal. It's pretty trivial in this case to see that if T
has the == operator then it must be true after assignment. So if this is a
serious proposal, it should come with a better motivating example.

I should say that these days I use code like the above with abandon -
depending upon the optimizer to drop any unused code. I don't think there's
anything wrong with that.

Robert Ramey

--
View this message in context: http://boost.2283326.n4.nabble.com/static-if-Is-there-interest-in-a-static-if-emulation-library-tp4667118p4667213.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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