[Issue 13428] Add template to perform appropriate substitution for inout when it appears in a type

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 17 01:02:50 UTC 2017


https://issues.dlang.org/show_bug.cgi?id=13428

Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras at gmail.com

--- Comment #1 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
import std.traits;

template SubstituteInout(FromType, ToType) {
    static if (is(ToType == inout(SubType), SubType)) {
        alias SubstituteInout = CopyTypeQualifiers!(FromType, SubType);
    } else static if (is(ToType == SubType*, SubType)) {
        alias SubstituteInout = SubstituteInout!(FromType, SubType)*;
    } else static if (is(ToType == SubType[], SubType)) {
        alias SubstituteInout = SubstituteInout!(FromType, SubType)[];
    } else static if (is(ToType == SubType[n], SubType, size_t n)) {
        alias SubstituteInout = SubstituteInout!(FromType, SubType)[n];
    } else static if (is(ToType == SubType[KeyType], SubType, KeyType)) {
        alias SubstituteInout = SubstituteInout!(FromType,
SubType)[SubstituteInout!(FromType, KeyType)];
    } else {
        alias SubstituteInout = ToType;
    }
}

unittest {
    static assert(is(SubstituteInout!(const(string), int) == int));
    static assert(is(SubstituteInout!(const(string), inout(int)[]) ==
const(int)[]));
    static assert(is(SubstituteInout!(const(string), inout(int)) ==
const(int)));
    static assert(is(SubstituteInout!(const(string), inout(int)*[][3][int]) ==
const(int)*[][3][int]));
    static assert(is(SubstituteInout!(const(string), inout(int)[inout(string)])
== const(int)[const(string)]));
}

--


More information about the Digitalmars-d-bugs mailing list