compile-time opIndex

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 18 07:47:24 PST 2014


On 12/18/14 10:43 AM, Steven Schveighoffer wrote:

> struct KeyValuePair(K, V)
> {
>     K key;
>     V value;
>     enum length = 2;
>     ref K opTupleIndex(int x) if(x == 0) { return key;}
>     ref V opTupleIndex(int x) if(x == 1) { return value;}
> }
>

Or with more recent changes to the compiler:

template opTupleIndex(int x) if (x == 0) { alias opTupleIndex = key;}
template opTupleIndex(int x) if (x == 1) { alias opTupleIndex = value;}

-Steve


More information about the Digitalmars-d mailing list