How to specify a template that uses unqualified type, like any normal function
Dominikus Dittes Scherkl via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 14 06:48:36 PDT 2017
if I use fixed-type functions, I can do the following:
uint foo(uint n)
{
++n; // modify n - as this function has received a copy of n,
this is always possible
return 42;
}
uint bar(const uint n)
{
assert(foo(n)==42);
return 17;
}
void main()
{
bar(3);
}
But if I try the same with a template parameter, it doesn't work:
import std.traits; // Unqual
uint foo(T)(Unqual!T n) // first try
{
++n; // modify should be possible
return 42;
}
uint foo2(T)(T n) // second try
{
++n; // modify fails, as T is const
return 42;
}
uint bar(T)(const T n)
{
assert(foo(n)==42u); // cannot deduce arguments - why?!?
assert(foo2(n)==42u); // here it can deduce the arguments, but
the function cannot modify n
return 17;
}
void main()
{
bar(3);
}
Any ideas what I need to do to make this work?
More information about the Digitalmars-d-learn
mailing list