www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about typeof(*this)

reply choi heejo <azurenote.enseed gmail.com> writes:
hi there, please read this code:

import std.stdio;

struct test(T)
{
alias typeof(*this) type;
T a;
}


void main()
{
test!(int).type t;
 t.a = 123;
 writeln(t.a);
}

I guessed it would work. but the compiler said:

main.d(7): Error: can only * a pointer, not a 'test!(int)'
main.d(14): Error: template instance main.test!(int) error instantiating

what is the problem?
Oct 16 2010
parent Ivo Kasiuk <i.kasiuk gmx.de> writes:
choi heejo wrote:
 hi there, please read this code:
=20
=20
 import std.stdio;
=20
=20
 struct test(T)
 {
 alias typeof(*this) type;
 T a;
 }
=20
=20
=20
=20
 void main()
 {
 test!(int).type t;
=20
 t.a =3D 123;
=20
 writeln(t.a);
 }
=20
=20
 I guessed it would work. but the compiler said:
=20
=20
 main.d(7): Error: can only * a pointer, not a 'test!(int)'
 main.d(14): Error: template instance main.test!(int) error
 instantiating
=20
=20
 what is the problem?
=20
In D2, "this" in a struct is a reference type, not a pointer type. So just replace "typeof(*this)" by "typeof(this)" and then it should work. Ivo
Oct 17 2010