Bug 4256 – Inner template mathods can't access this pointer

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-05-31T05:52:00Z
Last change time
2015-06-09T05:14:57Z
Keywords
wrong-code
Assigned to
nobody
Creator
simen.kjaras

Comments

Comment #0 by simen.kjaras — 2010-05-31T05:52:19Z
struct bar { int n; void baz( ) { void qux(T)() { n = 3; } qux!int(); } } The above code barfs on 'n = 3;': "Error: need 'this' to access member n".
Comment #1 by simen.kjaras — 2011-09-16T08:48:05Z
This bug has grown worse. A lot worse. Good news: this now compiles: struct foo { int n; void baz( ) { void qux(T)() { n = 3; assert(n == 3); // Passes } qux!int(); } } void main( ) { foo b; assert(b.n == 0); // Passes b.baz(); assert(b.n == 3); // Fails } As we can see, the change in n is not properly propagated. What's even worse, is this: struct foo { int n; void baz( ) { void qux(T)(void* a) { // Added parameter n = 3; assert(n == 3); // Passes assert(a == &this); // Added. } qux!int(&this); } } void main( ) { foo b; assert(b.n == 0); // Passes b.baz(); assert(b.n == 3); // No longer fails. } You can then remove the assert on line 7 (a == &this) to get the old behavior back, the additional parameter changes nothing on its own.