Implicit Template Parameters Cannot Decipher Aliases?

Simen Kjærås simen.kjaras at gmail.com
Wed Apr 25 10:25:11 UTC 2018


On Wednesday, 25 April 2018 at 07:39:28 UTC, Vijay Nayar wrote:
> I have encountered a problem where whenever I attempt to use a 
> templated function with alias that partially limits the type of 
> the arguments, the program fails to compile.  But if I avoid 
> using an alias, the same function can infer all arguments.
>
> Is this working as intended or have I encountered a bug or 
> missing feature?

It's a known issue, and could be solved in some cases by partial 
template expansion, which is currently not part of the language. 
I believe it's in bugzilla somewhere, but a cursory search 
yielded no results.

In the general case, the issue is unsolvable, since the 
relationship between template parameters and alias results may be 
arbitrarily complex. A simple degenerate case is this:

alias Foo(T) = string;

T fun(T)(Foo!T a) { return T.init; }

unittest {
     // What's val's type?
     auto val = fun("");
}

Since in this case Foo!T retains no information about T, it's 
impossible to figure out what T should be. In a more realistic 
case, where the alias template body includes static ifs, or there 
are overloads, the relationship is also hard to trace:

template Foo(T) if (T.sizeof < 4) {
     alias T[T.sizeof] Foo;
}
template Foo(T) if (T.sizeof >= 4) {
     alias T[T.sizeof/2] Foo;
}

T fun(T)(Foo!T a) { return T.init; }

unittest {
    int[2] a;
    auto val = fun(a);
}

Sure, for a human it's easy to figure out which overload is the 
right one, but defining the rules such that the compiler can 
reliably do it is harder.

--
   Simen


More information about the Digitalmars-d-learn mailing list