Boost logo

Boost Users :

Subject: Re: [Boost-users] C++ guru required!
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-02-19 13:58:36


> Assuming
> I'm correct and that Andrei's use cases don't make the case for
> static if - Is there a real case for static if.

Yes. Here's an example:

template <typename Iterator>
void advance(Iterator it, size_t n)
{
    static_if(iterator_category<it>::type == random_access_category_tag)
    {
        it += n;
    }
    else
    {
        for (size_t i = 0; i < n; ++i)
            ++it;
    }
}

Were one to use a runtime if instead, and instantiate the function with
an iterator that is not random access, one would get a compiler error
of the form 'no match for operator+=(Iterator, size_t)'.

The idea is that with a runtime if, the compiler type-checks the unused
branch even if it ends up eliminating it in later on in the optimization
phase.

Regards,
Nate
                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net