Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 31, 2010 overloading template functions it not always allowed | ||||
---|---|---|---|---|
| ||||
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 ...) Seems I cannot overload them just because these are template functions. But I dont see the rationale behind this. 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. |
December 31, 2010 Re: overloading template functions it not always allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to szali | On Fri, 31 Dec 2010 09:09:23 -0500, szali <bszalkai0@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 |
December 31, 2010 Re: overloading template functions it not always allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer:
> so even though I feel this is a bug (it should be silently ignored),
Generally silently ignoring attributes is exactly the opposite you want from a modern compiler. See bug 3934.
Bye,
bearophile
|
December 31, 2010 Re: overloading template functions it not always allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Fri, 31 Dec 2010 10:11:02 -0500, bearophile <bearophileHUGS@lycos.com> wrote:
> Steven Schveighoffer:
>
>> so even though I feel this is a bug (it should be silently ignored),
>
> Generally silently ignoring attributes is exactly the opposite you want from a modern compiler. See bug
> 3934.
In this case though, you are asking for a function which is already final to be final. The compiler can safely ignore the request because the request is already satisfied.
If you asked for a virtual function to be final, and the compiler ignored the request, I'd say it was bad.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation