Templated function as associative array property

bearophile bearophileHUGS at lycos.com
Sun Mar 7 16:27:10 PST 2010


> This problem might have been raised before, but I can't why this doesn't 
> compile (except the last line), while a non-template version of the same 
> code works well. Is this a bug?

I don't know why your code doesn't work, someone more expert than me can answer you. Maybe it's a bug.
In the meantime this works:

template Graph(T) {
    alias T[][T] Graph;

void add_edge(G, T)(ref G graph, T source, T target) if (is(G == Graph!T)) {
    graph[source] ~= target; // this is a slow operation

void main() {
    Graph!(string) graph;
    graph.add_edge("A", "B");


More information about the Digitalmars-d-learn mailing list