template interface and delegates

Justin Whear justin at economicmodeling.com
Mon Mar 31 13:20:31 PDT 2014


On Mon, 31 Mar 2014 18:58:30 +0000, anonymous wrote:

> Hi,
> I'm new to D and played a bit with templates and delegates.
> Now i discovered some behaviore that i don't understand. Can somebody
> explain me why i get two different outputs?
> 
> 
> import std.stdio;
> 
> 
> interface A(T){
> 	bool GetBool();
> 	T getT();
> }
> 
> class C:A!(double){
> 	override bool GetBool(){
> 		return false;
> 	}
> 	override double getT(){
> 		return 1;
> 	}
> }
> 
> void mwriteln(T)( A!T delegate() dg){
> 	writeln(dg().getT());
> }
> 
> void main()
> {
> 	auto c = new C(); writeln(c.getT()); mwriteln!double({return new
> 	C();});
> }

Looks like a bug.  Here's a cleaned-up test case:

-----------------------------------------------------------
interface A(T)
{
	T getT();
}

class C : A!double
{
	double getT(){ return 1; }
}

void mwriteln(T)(A!T delegate() dg)
{
	import std.stdio;
	auto a = dg();
	writeln("Checkpoint 1");
	assert(a !is null);
	writeln(a);
	writeln("Checkpoint 2");
}

void main()
{
	import std.traits;
	static assert(isImplicitlyConvertible!(C, A!double));
	mwriteln!double({return new C();});
}
-----------------------------------------------------------

Backtrace:
-----------------------------------------------------------
Program received signal SIGSEGV, Segmentation fault.
#0  0x000000000042ac8c in _d_interface_cast ()
#1  0x0000000000427ec6 in 
std.format.__T11formatValueTS3std5stdio4File17LockingTextWriterTC4test8__T1ATdZ1ATaZ.formatValue
() ()
#2  0x0000000000427e23 in 
std.format.__T13formatGenericTS3std5stdio4File17LockingTextWriterTC4test8__T1ATdZ1ATaZ.formatGeneric
() ()
#3  0x0000000000427d29 in 
std.format.__T14formattedWriteTS3std5stdio4File17LockingTextWriterTaTC4test8__T1ATdZ1AZ.formattedWrite
() ()
#4  0x0000000000427871 in 
std.stdio.File.__T5writeTC4test8__T1ATdZ1ATaZ.write() ()
#5  0x00000000004277d5 in std.stdio.__T7writelnTC4test8__T1ATdZ1AZ.writeln
() ()
#6  0x000000000042771f in test.__T8mwritelnTdZ.mwriteln() ()
#7  0x00000000004276a9 in D main ()
#8  0x000000000042b61c in rt.dmain2._d_run_main() ()
#9  0x000000000042b576 in rt.dmain2._d_run_main() ()
#10 0x000000000042b5dc in rt.dmain2._d_run_main() ()
#11 0x000000000042b576 in rt.dmain2._d_run_main() ()
#12 0x000000000042b4f7 in _d_run_main ()
#13 0x0000000000429c5f in main ()
-----------------------------------------------------------


More information about the Digitalmars-d-learn mailing list