www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - simple struct template

reply Hendrik Renken <funsheep gmx.net> writes:
Hi,

i've wrote a simple struct template:

struct Vector(T)
{
    T x;
    T y;
    T z;
}

now i would like to define some aliases to make my programming life easier:

Vectorf for Vector!(float)
Vectord for Vector!(double)

void main(char[][] args)
{
    Vectorf v;
    v.x = 0.2f;
}


is that possible? i tried

mixin Vector!(float) Vectorf;
alias Vector!(float) Vectorf;

with gdc 0.23. both dont work.

regards
Hendrik
Mar 30 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Hendrik Renken wrote:
 Hi,
 
 i've wrote a simple struct template:
 
 struct Vector(T)
 {
    T x;
    T y;
    T z;
 }
 
 now i would like to define some aliases to make my programming life easier:
 
 Vectorf for Vector!(float)
 Vectord for Vector!(double)
 
 void main(char[][] args)
 {
    Vectorf v;
    v.x = 0.2f;
 }
 
 
 is that possible? i tried
 
 mixin Vector!(float) Vectorf;
 alias Vector!(float) Vectorf;
 
 with gdc 0.23. both dont work.
 
 regards
 Hendrik

alias Vector!(float) Vectorf; alias Vector!(double) Vectord; Should work; if not, what error message is the compiler giving you? -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 30 2007
parent reply Hendrik Renken <funsheep gmx.net> writes:
 alias Vector!(float) Vectorf;
 alias Vector!(double) Vectord;
 
 Should work; if not, what error message is the compiler giving you?
 
 	-- Daniel
 

dmd on linux32, gdc 0.23 on mac os: "template instance forward reference to template declaration Vector(T)" hendrik
Mar 30 2007
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Hendrik Renken wrote:
 alias Vector!(float) Vectorf;
 alias Vector!(double) Vectord;

 Should work; if not, what error message is the compiler giving you?

"template instance forward reference to template declaration Vector(T)"

Put your aliases after the struct definition: struct Vector(T) { T x; T y; T z; } alias Vector!(float) Vectorf; void main(char[][] args) { Vectorf v; v.x = 0.2f; }
Mar 30 2007
parent reply Hendrik Renken <funsheep gmx.net> writes:
 Put your aliases after the struct definition:
 
 struct Vector(T)
 {
    T x;
    T y;
    T z;
 }
 
 alias Vector!(float) Vectorf;
 
 void main(char[][] args)
 {
    Vectorf v;
    v.x = 0.2f;
 }

ah, thanks for your help, now i understand the error message ^^. Need to bother you once again. how do i overload opCall, when i want to return the appropriate type, i tried the obvious (at least for me): struct Vector(T) { public: T x, y, z; static Vector!(T) opCall(T x, T y, T z) { this.x, y, z = (x, y, z); } } but then the compiler complains: function Vector.Vector!(float).Vector.opCall expected to return a value of type Vector!(float) where i have defined the aliases. regards, hendrik
Mar 30 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Hendrik Renken wrote:
 how do i overload opCall, when i want to return the appropriate type, i 
 tried the obvious (at least for me):
 
 
 struct Vector(T)
 {
 
     public: T x, y, z;
     
     static Vector!(T) opCall(T x, T y, T z)
     {
     this.x, y, z = (x, y, z);

change that to: Vector!(T) result; result.x = x; result.y = y; result.z = z; return result; Static member function don't _have_ a "this"
     }
     
 }
 
 
 but then the compiler complains:
 
 function Vector.Vector!(float).Vector.opCall expected to return a value 
 of type Vector!(float)

Because you didn't actually return anything.
Mar 30 2007
next sibling parent Hendrik Renken <funsheep gmx.net> writes:
Frits van Bommel wrote:
 Hendrik Renken wrote:
 how do i overload opCall, when i want to return the appropriate type, 
 i tried the obvious (at least for me):


 struct Vector(T)
 {

     public: T x, y, z;
         static Vector!(T) opCall(T x, T y, T z)
     {
     this.x, y, z = (x, y, z);

change that to: Vector!(T) result; result.x = x; result.y = y; result.z = z; return result; Static member function don't _have_ a "this" Because you didn't actually return anything.

*banging the head against the table* seems, as if my brain takes a long nap. sorry for bothering.
Mar 30 2007
prev sibling parent =?UTF-8?B?SmFyaS1NYXR0aSBNw6RrZWzDpA==?= <jmjmak utu.fi.invalid> writes:
Frits van Bommel wrote:
 Hendrik Renken wrote:
 how do i overload opCall, when i want to return the appropriate type,
 i tried the obvious (at least for me):


 struct Vector(T)
 {

     public: T x, y, z;
         static Vector!(T) opCall(T x, T y, T z)
     {
     this.x, y, z = (x, y, z);

change that to: Vector!(T) result; result.x = x; result.y = y; result.z = z; return result;

Also I think you can use just Vector instead if Vector!(T) in those routines. At least it works that way. Those extra letters are sooo expensive.
Mar 30 2007