Re: is() and const
On Wednesday, July 18, 2012 10:43:23 Andrea Fontana wrote: > So: > const(int) : int <-- true const int is implicitly convertible to int, because it's a value type, and assigning a const int to an int (or vice versa) makes a copy. > const(PP) : PP <-- false const PP is not implicitly convertible to PP, because it's a reference type. So, assigning a PP to a PP doesn't make a copy. It just copies the reference. And a const PP can't be converted to a mutable PP, because that would be dropping the const, invalidating const's guarantees. If PP were a struct with no pointers, arrays, or reference types (or it had a postblit constructor), then it would be a value type, and const PP _would_ implicitly convert to PP. > Is this behaviour correct? Yes. > And how can I check if T is of a certain class ignoring consts (and > avoiding double checks)? is(Unqual!T == T) Unqual is in std.traits. - Jonathan M Davis
Re: is() and const
It seems to works (but i use Unqual!T directly) Thank you :) Il giorno mer, 18/07/2012 alle 11.13 +0200, bearophile ha scritto: > Andrea Fontana: > > > const(int) : int <-- true > > const(PP) : PP <-- false > > > > Is this behaviour correct? > > I think it's correct, and it's caused by the difference between > value types and reference types. > > > > And how can I check if T is of a certain class ignoring consts > > (and avoiding double checks)? > > There are some different ways to do it, one of them is to use > something like (untested): > > is(Unqual!typeof(x) == PP) > > Where Unqual is in Phobos, std.traits.Unqual. > > Bye, > bearophile
Re: is() and const
Andrea Fontana: const(int) : int <-- true const(PP) : PP <-- false Is this behaviour correct? I think it's correct, and it's caused by the difference between value types and reference types. And how can I check if T is of a certain class ignoring consts (and avoiding double checks)? There are some different ways to do it, one of them is to use something like (untested): is(Unqual!typeof(x) == PP) Where Unqual is in Phobos, std.traits.Unqual. Bye, bearophile
is() and const
Run this code: class PP {} void what(T)(T val) { static if (is(T == int)) writeln ("T == int"); static if (is(T == const(int))) writeln ("T == const(int)"); static if (is(T : int)) writeln ("T : int"); static if (is(T == PP)) writeln ("T == PP"); static if (is(T == const(PP))) writeln ("T == const(PP)"); static if (is(T : PP)) writeln ("T : PP"); } void main(string[] args) { const int aa = 10; int ab; const PP ba = new PP; PP bb = new PP; writeln("- Testing const(int)"); what(aa); writeln(); writeln("- Testing int"); what(ab); writeln(); writeln("- Testing const(PP)"); what(ba); writeln(); writeln("- Testing PP"); what(bb); writeln(); return; } It says: - Testing const(int) T == const(int) T : int - Testing int T == int T : int - Testing const(PP) T == const(PP) - Testing PP T == PP T : PP So: const(int) : int <-- true const(PP) : PP <-- false Is this behaviour correct? And how can I check if T is of a certain class ignoring consts (and avoiding double checks)?