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

C++ 如何确定作为对象传递给另一个类的模板类的数据类型,c++,C++,这是我的密码。如何使A::type为int或double,或者其他用于生成类B实例的东西 这就行了 template<class X> class A { typedef typename X::type type; }; template<class Y> class B { public: typedef Y type; }; 这就行了 template<class X> class A { typedef typename X

这是我的密码。如何使A::type为int或double,或者其他用于生成类B实例的东西

这就行了

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

template<class Y>
class B
{
public:
    typedef Y type;
};
这就行了

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 <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;
}
也许这会有帮助

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;
}

这是你的新版本还是无关的?我很困惑,你能说得更清楚些吗?你到底在问什么?这是你的新版本还是无关的?我很困惑,你能说得更清楚些吗?你到底在问什么?当然,只有在声明A@RemyLebeau实例时实际使用B作为参数时,这才起作用。如果模板参数中有一个名为type的typedef,那么它就起作用了。部分专门化会更好吗?@DyP是的,如果像in的答案那样实现,它会更好。当然,这只会在声明A的实例时使用B作为参数时起作用。@RemyLebeau如果模板参数中有一个名为type的typedef,它就会起作用。部分专门化会更好吗?@DyP是的,如果像in的答案那样实现的话会更好。你真的能从这样的答案中派生出A吗?@RemyLebeau当然可以。它也是在年完成的。你真的能从那样的故事中衍生出一个故事吗?@RemyLebeau,你当然能。这件事也做得很好。