Out of bound problem
Christopher Wright
dhasenan at gmail.com
Sun Feb 17 07:25:30 PST 2008
bearophile wrote:
> Thanks to everybody, in my code I did solve the problem like this:
> result = foo(a, b.dup[0]);
>
> But I was curious to know what you think about this situation. Now I know it's probably a compiler bug.
>
>
> Christopher Wright:
>> Y'know, there's a template for this in std.traits and tango.core.Traits.
>
> I don't use Tango (yet). I use always the std lib when possible, I know there's no point in duplicating the std lib. I don't remember the bugs or the problems, but I have found std.traits not enough for similar array purposes, and I have found a bug in the traits of Tango to do that array purposes, so I have created IsArray and IsDynamicArray, you can find them in my d libs:
std.traits.isArray/isStaticArray/isDynamic doesn't work? I don't recall
seeing such a bug report.
> template IsArray(T) {
> const bool IsArray = is(typeof(T.length)) && is(typeof(T.sort)) &&
> is(typeof(T.reverse)) && is(typeof(T.dup));
> }
> template IsDynamicArray(T) {
> const bool IsDynamicArray = is( typeof(T.init[0])[] == T );
> }
> template IsStaticArray(T) {
> const bool IsStaticArray = IsArray!(T) && (!IsDynamicArray!(T));
> }
const bool IsStaticArray(T) {
const bool IsStaticArray = is(typeof(T[0])) &&
is(typeof(T[0])[T.sizeof/T[0].sizeof] == T);
}
Then, since IsStaticArray doesn't depend on IsArray, you can change
IsArray to return true iff IsStaticArray or IsDynamicArray.
> That IsArray may look a bit like "duck typing at compile time", but that's the only thing that works that I have found.
> I use similar tools all the time, so I need to have them quite sharp :-)
>
> Bye,
> bearophile
More information about the Digitalmars-d-learn
mailing list