www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function template conflicts

reply Henrik Eneroth <Henrik_member pathlink.com> writes:
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
Jun 29 2006
parent Henrik Eneroth <Henrik_member pathlink.com> writes:
Also... DDoc seems to ignore documentation for function templates. An example of
how I am currently doing it:

****************************************************************************
* Create a premultiplied colour based on the given colour.
*
* Params:
*  colour = A colour [rgba!(datatype)] containing the red, green, blue and alpha
values you wish to use.
***************************************************************************/
rgbaType rgba_pre(rgbaType)( rgbaType colour )
{
rgbaType tmp = new rgbaType(colour);
tmp.premultiply();
return tmp;
}

Unfortunately, this shows up as absolutely nothing in the documentation. Any
ideas?


Regards,

Henrik
Jun 29 2006