Constant template arguments
Meta via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jan 3 22:39:38 PST 2015
On Sunday, 4 January 2015 at 01:34:51 UTC, Manu via Digitalmars-d
wrote:
> I kinda feel something like this ought to work, but I can kinda
> see
> why it doesn't...
>
> void foo(T = Unqual!U, U)(T a, T b);
>
> Thing is, it perceives that 'T' as typed by the argument
> received *is*
> T, but it's not; T is already assigned a type. It would need to
> see
> that T is a function of U which is unknown (available to be
> inferred),
> and to forward the incoming type to U, such that T's function
> can be
> applied to it.
>
> ...yeah, it would need to transfer the type received to U,
> because T's
> type is already assigned as an explicit function of unknown U.
>
> I'm not sure if that made sense, but I can visualise the
> process. I'm
> sure it *could* work, but would it be sturdy?
>
> I've run into this many times in the past too... never really
> thought
> on it whether it's a problem that could actually be solved.
This works for mutable, const, and immutable scalar types.
import std.traits;
U fun(T: const(U), U)(T val)
if (isScalarType!T)
{
return val;
}
void main()
{
const int n = 10;
static assert(is(typeof(n.fun) == int));
int m = 10;
static assert(is(typeof(m.fun) == int));
immutable int x = 10;
static assert(is(typeof(x.fun) == int));
}
More information about the Digitalmars-d
mailing list