Sergio Martins February 2016

Get type of outer class in template

struct Outer {
    struct Inner {
    };
};

template <typename T>
void test()
{
    ??Outer?? foo; // How do I get the type of T's outer class ?
}

test<Outer::Inner>();

Inside test() I have a template argument of type T and I want to declare a variable of whatever type is the outer class of T.

I think this should be trivial for the compiler to get that info, but so far couldn't find how to do it.

Answers


Brian February 2016

It can't be done. There is indeed much information available to the compiler that cannot be easily used in a metaprogramming context. Ongoing work on adding compile-time reflection to the language might eventually improve this situation, but don't hold your breath.

In the meantime, you'll just have to do it the old-fashioned way.

struct Outer {
    struct Inner {
        typedef Outer outer_class;      
    };
}

Post Status

Asked in February 2016
Viewed 2,821 times
Voted 8
Answered 1 times

Search




Leave an answer