Function overloading

Regan Heath regan at
Tue Jul 31 02:05:22 PDT 2007

Vladimir wrote:
> Hi there,
> Why not to allow template function overloading? Like
> void test (T) (T)
> {
> }
> void test (int)
> {
> }

It is:

import std.stdio;

void test(T)(T p)
	writefln("misc called");

void test(T : int)(T p)
	writefln("int called");

void main()
	int i;
	long l;
	float f;

int called
misc called
misc called

However, without the !(int) etc it fails to compile with: "template 
tplspec.test(T : int) specialization not allowed for deduced parameter T"

Given the documentation:

Argument Deduction

The types of template parameters are deduced for a particular template 
instantiation by comparing the template argument with the corresponding 
template parameter.

For each template parameter, the following rules are applied in order 
until a type is deduced for each parameter:

    1. If there is no type specialization for the parameter, the type of 
the parameter is set to the template argument.
    2. If the type specialization is dependent on a type parameter, the 
type of that parameter is set to be the corresponding part of the type 
    3. If after all the type arguments are examined there are any type 
parameters left with no type assigned, they are assigned types 
corresponding to the template argument in the same position in the 
    4. If applying the above rules does not result in exactly one type 
for each template parameter, then it is an error.

It seems that for the first call "foo(i)" step #1 does nothing, step #2 
sets the type to 'int', step #3 does nothing and step #4 finds that 
there _is_ exactly 1 type per argument so why the error?

Is this a bug?


More information about the Digitalmars-d-learn mailing list