On 1/18/2014 2:16 PM, Michel Fortin wrote:
It works, up to a point.
void foo(RangedInt!(0, 5) a);
void bar(RangedInt!(0, 10) a)
{
if (a < 5)
foo(a); // what happens here?
}
In that "foo(a)" line, depending on the implementation of RangedInt you either
get a compile-time error that RangedInt!(0, 10) can't be implicitly converted to
RangedInt!(0, 5) and have to explicitly convert it, or you get implicit
conversion with a runtime check that throws.
Yes, and I'm not seeing the problem. (The runtime check may also be removed by
the optimizer.)
Just like pointers, not knowing about the actual control flow pushes range
constrains enforcement at runtime in situations like this one.
With pointers, the enforcement only happens when converting a pointer to a
nonnull pointer.
In fact, even the most obvious case can't be caught at compile-time with the
template approach:
void baz()
{
foo(999); // runtime error here
}
Sure it can. Inlining, etc., and appropriate use of compile time constraints.