Parameters declared as the alias of a template won't accept the arguments of the same type.
Loara
loara at noreply.com
Tue May 3 09:56:22 UTC 2022
On Monday, 2 May 2022 at 17:21:53 UTC, JG wrote:
> 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
>> }
>> ```
>
>
> I don't really see what your example is trying to show. This
> also doesn't work,
> and in my mind should be equivalent:
> ```d
> T simp(T)(int val) {
> return T.init;
> }
>
> int main() {
> simp(3);//Impossible to deduce T
> }
> ```
Yeah I know, but I'm trying to show that allowing aliased
templated function parameters will bring many bugs in user code,
especially with aliases that alias another aliased declarations.
I think using
[typedefs](https://dlang.org/phobos/std_typecons.html#Typedef)
and template constraints is simpler and make your code more
readable too.
More information about the Digitalmars-d-learn
mailing list