Template constraints: opCmp and opUnary!"++"

Francesco Cattoglio francesco.cattoglio at gmail.com
Fri Dec 20 07:38:55 PST 2013


I'm trying to experiment a bit around the iota function.
If I try to impose the following constraits:

auto my_iota(B, E)(B begin, E end)
if (is (typeof(++begin)) && is (typeof(begin < end))) {}

Everything works as it should, but according to "D Templates: A 
Tutorial" book, you should not use arguments in constraints.
If I try doing something like:

auto my_iota(B, E)(B begin, E end)
if (is (typeof(++B.init)) && is (typeof(B.init < E.init))) {}

the code stops compiling for integers.
On the other hand the code

auto my_iota(B, E)(B begin, E end)
if (is (typeof(++B)) && is (typeof(B < E))) {}

fails to compile for both integers and my defined types.
I read the "D Templates: A Tutorial" book and as far as I can 
tell "++B.init" and "B.init < E.init" doesn't look too much 
wrong, but I've not seen any constraint of this kind in phobos 
(using variables instead of types) so I was wondering if doing 
something like this is actually bad or even really bad. (And I 
also wonder how to properly setting those constraints directly on 
types)


More information about the Digitalmars-d-learn mailing list