
Nathan Ridge wrote:
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.
Well, this seems like a better example, but ... the following wouldn't compile anyway as if can't compare types. if(iterator_category<it>::type == random_access_category_tag) I'm not sure as to whether the proposal actually expands if to compare types. Anyway, one could right right now: if(is_random_access_iterator<Iterator>::value){ it += n; } else{ for(size_ti = 0; i < n; ++i){ ++it; } IF the compiler skipped the "false" branch. I would guess this behavior is undefined (but maybe not). So maybe everything would be just fine if behavior in this case were defined to skip branches known to be false at compile time. Of course this presumes that there iterator traits implemented as integral boolean compile time constants. But this is no more burdensome comparing tag types Actually, I did look at the proposal and it includes a bunch of new syntax not explained in Andrei's talk. (that doesn't really convince me either - but of course that's off topic). Anyway, I did get what i wanted from my question - I can just use if(... dependent on a template paramter) whereever it makes syntactical sense and get what I expect to get. Thanks to all who participated in this thread.
Regards, Nate =