Function template conflicts

Henrik Eneroth Henrik_member at pathlink.com
Thu Jun 29 04:34:19 PDT 2006


I have been playing around with function templates and now I have a question.
Take a look at the code here (or just skip it and read the actual question
below):


/* CODE */
import std.stdio;

/******************** 
* Basic colour-class
*******************/
class rgba(T)
{
public:

alias rgba!(T) thisType;
T r, g, b, a;

this()
{
r = g = b = a = 0;
}

this( T red, T green, T blue, T alpha = 1.0 )
{
r = red;
g = green;
b = blue;
a = alpha;
}

this( thisType colour )
{
r = colour.r;
g = colour.g;
b = colour.b;
a = colour.a;
}	

void premultiply()
{
writefln("Premultiply! R:", r, " G:", g, " B:", b, " A:", a);
}
}


/********************************************************* 
* Takes red, green, blue and alpha values, premultiplies, 
* and returns a colour. 
********************************************************/
rgba!(T) rgba_pre(T)( T red, T green, T blue, T alpha=1 )
{
rgba!(T) tmp = new rgba!(T)(red, green, blue, alpha);
tmp.premultiply();
return tmp;
}


/************************************
* Takes a colour, premultiplies, 
* and returns the new colour. 
***********************************/
rgbaType rgba_prex(rgbaType)( rgbaType colour )
{
rgbaType tmp = new rgbaType(colour);
tmp.premultiply();
return tmp;
}

void test( int k )
{
writefln( k );
}

void test()
{
writefln( 0 );
}

int main()
{
alias rgba!(double) colour;
colour col = new colour(0.5, 0.5, 0.5);

test(); 
test( 8 );
/* Works */		

rgba_pre(1.0, 1.0, 1.0);
/* Works! Neat! */

rgba_prex( col );
/* 
* Works (neat!), but only if this function template is named something else than
rgba_pre 
* Otherwise: test.d(45): template test.rgba_pre(rgbaType) conflicts with
test.rgba_pre(T) at test.d(38)
*/

return 1;
}
/* /CODE */

It seems that I cannot let both function templates have the same name even
though they take different arguments, something that works with normal functions
(because they are mangled to different names during compilation perhaps?).
It's not a big problem, but I am curious to know why and if there is any way to
make this happen regardless.

Regards,

Henrik





More information about the Digitalmars-d-learn mailing list