toString() on struct template

mario pernici mario.pernici at mi.infn.it
Wed Feb 28 13:43:45 PST 2007


Henning Hasemann Wrote:

> I have a struct template with just 2 data members
> (thats why I'd actually preferred a struct over a class).
> 
> It implements several methods such as overloaded operators
> and the pseudo constructor roughly like this:
> 
> struct Vector2d(T) {
> 	T x, y;
> 	Vector2d!(T) opCall(T x, T y) {
> 	  Vector2d!(T) v;
> 	  v.x = x;
> 	  v.y = y;
> 	  return v;
> 	}
> 	char[] toString() {
> 	  return format("(%d|%d)", x, y);
> 	}
> }
> 
> But the toString actually crashes hard everytime *before* the format-call.
> (Btw: Dont be afraid because of the %d's I actually only instantiate the
> template with int for testing).
> 
> Is there something wrong with my understanding or with the compiler
> (tested dmd 1.005 and 1.007)?
> 
> Whats the proper way of implementing toString vor structs if not like this?
> 
> Henning

It works in this example 

void main()
 {
     Vector2d!(int) a;
     auto b = a(5,6);
     writefln(b);
 }

output: (5|6)

Mario





More information about the Digitalmars-d mailing list