Parameters declared as the alias of a template won't accept the arguments of the same type.

Elfstone elfstone at yeah.net
Tue May 3 06:20:53 UTC 2022


On Monday, 2 May 2022 at 16:29:05 UTC, Loara wrote:
> On Sunday, 1 May 2022 at 03:57:12 UTC, Elfstone wrote:
>> [...]
>
> Template deduction for aliased function parameter is a very 
> tricky argument and it's not so simple to handle in certain 
> cases. Consider for example this code:
>
> ```d
>     template MyAlias(T){
>       alias MyAlias = int;
>     }
>
>     T simp(T)(MyAlias!T val){
>       return T.init;
>     }
>
>     int main(){
>       simp(3);//Impossible to deduce T
>       simp( cast(MyAlias!string) 4);//Also invalid since 
> MyAlias!string is exactly int
>       simp!string(4);//Ok, no parameter deduction
>     }
> ```
>
> Instead to use aliases it's better (both in D and in C++) to 
> use constraints/concepts.

Yeah, I understand some cases are impossible, and to be avoided. 
I believe your example is also impossible in C++, but it's better 
the compiler do its job when it's totally possible - needless to 
say, C++ compilers can deduce my _dot_.
Constraints/Concepts are useful, but what's needed here is a 
literal _alias_. There's no ambiguity, no extra template 
parameters introduced in the declaration.


More information about the Digitalmars-d-learn mailing list