Fun with templates

TommiT tommitissari at hotmail.com
Sat Jul 6 07:35:59 PDT 2013


On Saturday, 6 July 2013 at 13:30:02 UTC, TommiT wrote:
> 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)
> }

Some more details of a bit more complicated use case:

E foo(inout T : E[3], E)(T arr)
{
     return arr[0];
}

E bar(inout T : inout(E)[3], E)(T arr)
{
     return arr[0];
}

void main()
{
     immutable int[3] iarr;

     foo(iarr); // returns an immutable int
     bar(iarr); // returns an int
}


More information about the Digitalmars-d mailing list