overloading template functions it not always allowed

Steven Schveighoffer schveiguy at yahoo.com
Fri Dec 31 06:56:24 PST 2010


On Fri, 31 Dec 2010 09:09:23 -0500, szali <bszalkai0 at gmail.com> wrote:

BTW, this list is generally not used for questions (it's auto-generated  
 from bugzilla reports), d.learn is a better place, but no worries, here  
are your answers:

> In one of my classes, I created two overloads to opIndexAssign (the
> second one was made for better performance, because in most cases
> only one index is used):
>
> public final T opIndexAssign(T value, int[] args ...)
> public final T opIndexAssign(T value, int i)
>
> These are allowed by the compiler. But these are not:
>
> public T opIndexOpAssign(string op)(T value, int i)
> public T opIndexOpAssign(string op)(T value, int[] args ...)

It's a limitation of the way templates are specified.  To the compiler,  
both are the same template.

The way around this is to change the template parameters:

public T opIndexOpAssign(string op)(T value, int i)
public T opIndexOpAssign(string op, bool variadic=true)(T value, int[]  
args ...)

It's a crappy requirement, I think this is a well-known bug.

BTW, you gain very very very little by having both these functions, the  
variadic one is all you need.

Also, you may have an issue with using a variadic, as I think you can call  
with zero indexes (not sure how that would look).  You may want to replace  
both with this one function:

public T opIndexOpAssign(string op)(T value, int idx0, int[] idxN...)

> And when I want to make these "final", like this:
>
> public final T opIndexOpAssign(string op)(T value, int i)
>
> the compiler complains because it thinks i want to apply the final
> keyword to "string op" (why would I? the keyword is at a completely
> different position). That is kind of strange.

All template functions are final.  They cannot be virtual, so even though  
I feel this is a bug (it should be silently ignored), you can fix it by  
just removing final.

-Steve


More information about the Digitalmars-d-bugs mailing list