View mode: basic / threaded / horizontal-split · Log in · Help
October 17, 2010
Question about typeof(*this)
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?
October 17, 2010
Re: Question about typeof(*this)
choi heejo wrote:
> 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?
> 

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
Top | Discussion index | About this forum | D home