How to call a templated overload of opIndex

Ali Çehreli acehreli at yahoo.com
Fri Aug 15 21:09:50 UTC 2025


On 8/15/25 10:38 AM, Marc wrote:
 > Hello,
 > In my typed data frames I've this type of code. It works for default
 > value (float) but I can't call `opIndex!double[i, j]` or `opIndex!
 > string[i,j]`.

Calling it that way feels counter to operator overloading: It's supposed 
to allow the [i, j] syntax. Additionally, it is not clear what the 
meaning of !double vs. !float is.

Wouldn't it be a bug to call !double when the data is float?

 > It just doesn't work and I get the error `is not a
 > template declaration, it is a variable`.
 >
 > ```d
 > // Overload [i, j]
 > ref T opIndex(T = float)(size_t i, size_t j) const {

Related to my concern above, returning a ref to double would be wrong 
when the data is float.

 >     alias SliceType = Slice!(T*);
 >     // colValues is of type void*[], i cast it to a slice type and get
 >     // the value at index i (row index)
 >     auto values = cast(SliceType*) colValues[j];

Perhaps my concern in not valid because perhaps that cast is producing a 
new container of e.g. doubles. But then it would be inefficient to 
convert all elements to double.

Further, returning a reference to an element of a newly created 
container doesn't feel useful.

 >     return (*values)[i];
 > }
 > ```
 >
 > Any help would be really appreciated.

This design doesn't seem useful. How about something like the following:

import std.conv : to;

     x[i, j].to!double;
     y[i, j].to!float;

Ali



More information about the Digitalmars-d-learn mailing list