How to extract the AA type?

Steven Schveighoffer schveiguy at yahoo.com
Wed Dec 13 16:00:32 UTC 2017


A nice puzzle for those template gurus out there.

I have a function like this:

auto foo(T: V[K], V, K)(T t)
{
    RealAAType!(T, V, K) aa = t;
    return aa;
}

So I need to know what to write RealAAType. What I'm looking for is a 
mechanism to write the exact AA type that is passed in. RealAAType 
should be an AA, and nothing else.

Here are 2 things that *don't* work:

alias RealAAType!(T, V, K) = T;

This doesn't work because T could be an alias this'd struct. I don't 
want T, I want the actual AA type.

alias RealAAType!(T, V, K) = V[K];

This doesn't work when the T has a type modifier. For instance if T is 
const(int[int]), then V[K] is really const(int)[int], and you can't 
assign it to t.

Here is are the tests I want to compile:

const(int[int]) x;
auto aa1 = foo(x);
static assert(typeof(aa1) == const(int[int]));

static struct S
{
    int[int] aa;
    alias aa this;
}

const(S) s;
auto aa2 = foo(s);
static assert(typeof(aa2) == const(int[int]));

-Steve


More information about the Digitalmars-d mailing list