Alpharius
Alpharius

Reputation: 509

How do I find out the type of a class instance template?

I needed to find out the type of class template.

m_class <int> temp{};

Is it possible to understand that it has an 'int' or any other type ? I tried

decltype( temp)

But it returns

m_clas<int>

I will be glad of your help.

Upvotes: 0

Views: 51

Answers (2)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 123439

If you can modify m_class you can add an alias:

template <typename T>
struct m_class {
    using type = T;
};

If not you can write a type trait:

template <typename T>
struct m_class_type;

template <typename T>
struct m_class_type<m_class<T>> {
    using type = T;
};

Example:

int main() {
  m_class<int>::type f;
  m_class_type<m_class<int>> g;
}

f and g are both int.

Upvotes: 3

m88
m88

Reputation: 1988

If you made m_class yourself, you have to keep track of T yourself:

    template <class T>
    class m_class {
    public:
        using type_t = T;
    };

    //...

    m_class<int>::type_t myInt = 42;

STL containers usually keep the element type info this way, e.g. std::vector<int>::value_type.

Upvotes: 1

Related Questions