如何确定作为对象传递给另一个类的模板类的数据类型

how to determine the data type of a template class passed as object to another class

本文关键字:另一个 数据类型 何确定 对象      更新时间:2023-10-16

这是我的代码。我如何使A::type成为intdouble或其他用于创建B类实例的东西?

template<class X>
class A
{
typedef "*****" type
........
.....
......
}
template<class Y>
class B
{
......
.......
....
}
int main()
{
B<int> x;
A<B<int> > y;
.....
....
....
}

可以了

template<class X>
class A
{
    typedef typename X::type type;
};
template<class Y>
class B
{
public:
    typedef Y type;
};

也许像这样:

template <typename T>
struct B
{
    typedef T type;
    // ...
};
template <typename> struct A { /* ... */ };

typedef B<int> MyB;
int main()
{
    MyB          x;
    A<MyB::type> y;
}

也许这会有帮助。

template <class T>
class B;
template <class T>
class A {
    public:
        typedef T type;
};
template <class T>
class A<B<T>> : public A<T> {};
template <class T> class B {};
int main()
{
    A<B<int>>::type x;
}