Thread overview
simple struct template
Mar 30, 2007
Hendrik Renken
Mar 30, 2007
Daniel Keep
Mar 30, 2007
Hendrik Renken
Mar 30, 2007
Hendrik Renken
Mar 30, 2007
Frits van Bommel
Mar 30, 2007
Hendrik Renken
March 30, 2007
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
March 30, 2007

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/
March 30, 2007
> 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
March 30, 2007
Hendrik Renken wrote:
>> alias Vector!(float) Vectorf;
>> alias Vector!(double) Vectord;
>>
>> Should work; if not, what error message is the compiler giving you?
>>
> dmd on linux32, gdc 0.23 on mac os:
> "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;
}
March 30, 2007
> 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
March 30, 2007
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.
March 30, 2007
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.
March 30, 2007
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.