Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 27, 2013 template operator overload | ||||
---|---|---|---|---|
| ||||
Just out of curiosity: Is it possible to call an overloaded operator with a template type? ---- import std.stdio; struct A { void opIndex(T)(size_t index) { } } void main() { A a; a.opIndex!int(0); // [1] a!int[0]; // [2] } ---- [1] works, but [2] fails. How can I call opIndex with bracket syntax and a typename? Or is this not possible? |
November 27, 2013 Re: template operator overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 2013-11-27 16:07:50 +0000, Namespace said:
> Just out of curiosity: Is it possible to call an overloaded operator with a template type?
> ----
> import std.stdio;
>
> struct A {
> void opIndex(T)(size_t index) {
>
> }
> }
>
> void main() {
> A a;
> a.opIndex!int(0); // [1]
> a!int[0]; // [2]
> }
> ----
>
> [1] works, but [2] fails.
> How can I call opIndex with bracket syntax and a typename? Or is this not possible?
You need to simply use template deduction, and cast the parameter to what you're wanting to use between the brackets.
a[cast(int)0] (although zero is already an int);
|
November 27, 2013 Re: template operator overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shammah Chancellor | On Wednesday, 27 November 2013 at 17:25:49 UTC, Shammah Chancellor wrote:
> On 2013-11-27 16:07:50 +0000, Namespace said:
>
>> Just out of curiosity: Is it possible to call an overloaded operator with a template type?
>> ----
>> import std.stdio;
>>
>> struct A {
>> void opIndex(T)(size_t index) {
>>
>> }
>> }
>>
>> void main() {
>> A a;
>> a.opIndex!int(0); // [1]
>> a!int[0]; // [2]
>> }
>> ----
>>
>> [1] works, but [2] fails.
>> How can I call opIndex with bracket syntax and a typename? Or is this not possible?
>
> You need to simply use template deduction, and cast the parameter to what you're wanting to use between the brackets.
>
> a[cast(int)0] (although zero is already an int);
Inside the brackets I want an int. But I also want an extra type.
E.g.
----
A a;
a!string[4];
----
|
November 27, 2013 Re: template operator overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Namespace:
> void main() {
> A a;
> a.opIndex!int(0); // [1]
> a!int[0]; // [2]
> }
> ----
>
> [1] works, but [2] fails.
> How can I call opIndex with bracket syntax and a typename? Or is this not possible?
I think that's not supported by D syntax. So if you want that, you need to use a syntax like "a.at!int(0)".
Bye,
bearophile
|
November 27, 2013 Re: template operator overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 27 November 2013 at 17:47:13 UTC, bearophile wrote:
> Namespace:
>
>> void main() {
>> A a;
>> a.opIndex!int(0); // [1]
>> a!int[0]; // [2]
>> }
>> ----
>>
>> [1] works, but [2] fails.
>> How can I call opIndex with bracket syntax and a typename? Or is this not possible?
>
> I think that's not supported by D syntax. So if you want that, you need to use a syntax like "a.at!int(0)".
>
> Bye,
> bearophile
Too bad. Would have been really awesome.
|
November 27, 2013 Re: template operator overload | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Wednesday, 27 November 2013 at 18:40:09 UTC, Namespace wrote:
> Too bad. Would have been really awesome.
It is pretty hard to define within common grammar rules because of implicit nature of operators.
|
Copyright © 1999-2021 by the D Language Foundation