Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access nested template types

Tags:

c++

templates

Let's say there is a class A and MyType

template<typename DataType>
class MyType {
...
}

template<typename MyType>
class A {
...
}

When I create an instance of A with A<MyType<int>> how can I access the template type int inside A?

like image 522
Gustavo Avatar asked Jan 05 '23 09:01

Gustavo


1 Answers

Expose a type alias to the user:

template<typename DataType>
class MyType {
public:
    using InnerDataType = DataType;
};

template<typename MyType>
class A {
public:
    using InnerType = MyType;
};

Usage:

using MyA = A<MyType<int>>;
static_assert(std::is_same<
    typename MyA::InnerType::InnerDataType,
    int>{});

live example on wandbox

like image 179
Vittorio Romeo Avatar answered Jan 11 '23 18:01

Vittorio Romeo