Boost logo

Boost :

Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: Kyle Lutz (kyle.r.lutz_at_[hidden])
Date: 2014-09-03 12:11:40


On Tue, Sep 2, 2014 at 10:19 PM, Robert Ramey <ramey_at_[hidden]> wrote:
> 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.

Despite your expectations, this is not how optimizers work (or are
allowed to work). Would you expect the following to compile?

int main()
{
    if(1 == 2){
        !@#$%^&*();
    }
    return 0;
}

-kyle


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