is pointer

Jacob Carlborg doob at me.com
Sat Mar 20 13:20:19 PDT 2010


On 2010-03-20 00.29, bearophile wrote:
> (I am looking for rough corners in D, or in my knowledge of D.)
>
> In this page:
> http://www.digitalmars.com/d/2.0/templates-revisited.html
>
> In the section "Template Parameters" there is written:
>
> P:P*,         // P must be a pointer type
>
> -----------------
>
> So I have written this D2 program:
>
>
> template IsPointer1(T) {
>      enum bool IsPointer1 = is(T : T*);
> }
> void main() {
>      int* ptr;
>      static assert(IsPointer1!(typeof(ptr))); // Err
> }
>
>
> But it asserts, do you know why?

You can do like this:

template IsPointer (T)
{
	enum IsPointer = false;
}

template IsPointer (T : T*)
{
	enum IsPointer = true;
}

> -----------------
>
> Then I have written a different program:
>
>
> template IsPointer2(T) {
>      enum bool IsPointer2 = is(typeof(*T)) || is(T == void*);
> }
> void main() {
>      int* ptr;
>      static assert(IsPointer2!(typeof(ptr))); // OK
>      int[] arr;
>      static assert(!IsPointer2!(typeof(arr))); // Err
> }
>
>
> Do you know why IsPointer2 is true for the dynamic array type too?
>
> (I know in std.traits of Phobos 2 there is an isPointer).
>
> Thank you,
> bye,
> bearophile



More information about the Digitalmars-d-learn mailing list