Constant template arguments

Manu via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 3 17:34:41 PST 2015


On 3 January 2015 at 22:25, bearophile via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> This doesn't compile because T is const(int) so you can't modify the
> arguments a and b, despite they are values:
>
>
> void foo(T)(T a, T b) {
>     a = b;
>     b = a;
> }
> void main() {
>     const int x, y;
>     foo(x, y);
> }
>
>
> To make that code work I'd like to write something like this:
>
> void foo(T)(Deconst!T a, Deconst!T b) {
>
> Or this:
>
> void foo(T)(@deconst T a, @deconst T b) {
>
> Bye,
> bearophile

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.


More information about the Digitalmars-d mailing list