Implicit Template Parameters Cannot Decipher Aliases?
Vijay Nayar
madric at gmail.com
Wed Apr 25 07:39:28 UTC 2018
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?
Example below:
```
/**
* Demonstrate an unexpected compiler error when using implicit
template parameters
* combined with aliases to partially instantiate templates.
*/
struct Vector(ElemT, size_t SizeV) {
private:
ElemT[SizeV] _data;
public:
@property
ElemT[SizeV] data() {
return _data;
}
ElemT opIndex(size_t i) {
return _data[i];
}
}
// A helper alias to partially set a portion of compile-time
arguments.
template Vector3(ElemT) {
alias Vector3 = Vector!(ElemT, 3);
}
T addAllWithAlias(T)(Vector3!T v1) {
T sum = 0;
foreach (T v; v1.data) {
sum += v;
}
return sum;
}
T addAll(T)(Vector!(T, 3) v1) {
T sum = 0;
foreach (T v; v1.data) {
sum += v;
}
return sum;
}
void main() {
auto v1 = Vector3!double([1.0, 2.0, 3.0]);
assert(v1[1] == 2.0);
addAllWithAlias(v1); // Error!
// template.d(35): Error: template template.addAllWithAlias
cannot deduce function from
// argument types !()(Vector!(double, 3LU)), candidates are:
// template.d(24):
template.addAllWithAlias(T)(Vector3!T v1)
addAll(v1); // OK.
}
```
More information about the Digitalmars-d-learn
mailing list