template operator overload

Shammah Chancellor anonymous at coward.com
Wed Nov 27 09:25:48 PST 2013


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);




More information about the Digitalmars-d-learn mailing list