Fun with templates

TommiT tommitissari at hotmail.com
Sat Jul 6 06:30:01 PDT 2013


On Saturday, 6 July 2013 at 12:51:18 UTC, TommiT wrote:
> On Saturday, 6 July 2013 at 11:35:28 UTC, Manu wrote:
>> The way that makes the most sense to me is:
>>
>> void f(T)(Unqual!T t) {}
>
> But my syntax makes pretty much sense too:
>
> void foo(inout T)(T var)
> {
>     var = 42;
> }
>
> void main()
> {
>     foo!(int)(1);
>     foo!(const int)(2);
>     foo!(immutable int)(3);
> }

Some more details of this proposed feature:

inout(T) foo(inout T)(T a, T b)
{
     return var;
}

void bar(inout T)(T a, T b)
{
     a = b;
}

void main()
{
     const int con;
     immutable int imm;

     foo(con, con); // OK (returns const int)
     foo(con, imm); // Error: returned inout(T) is ambiguous

     bar(con, con); // OK
     bar(con, imm); // OK (no ambiguity because inout not used)
}


More information about the Digitalmars-d mailing list