考慮以下使用的模板,模板參數...如何申報/定義模板與模板參數的一類,而無需使用額外的模板參數
#include <iostream>
template <typename X>
class A
{
X _t;
public:
A(X t)
:_t(t)
{
}
X GetValue()
{
return _t;
}
};
template <typename T, template <typename T> class C >
class B
{
C<T> _c;
public:
B(T t)
:_c(t)
{
}
T GetValue()
{
return _c.GetValue();
}
};
using namespace std;
int main()
{
B<int, A> b(10);
cout<<b.GetValue();
return 0;
}
有沒有一種方法,通過該模板參數T可以去掉?例如,是否有辦法做以下工作?
//Does not compile
template <template <typename T> class C >
class B
{
C _c;
public:
B(T t)
:_c(t)
{
}
T GetValue()
{
return _c.GetValue();
}
};
int main()
{
B< A<int> > b(10);
cout<<b.GetValue();
return 0;
}
感謝您的回答,儘管我最終自己發現了這個。我正在編輯問題併發布我的工作,但顯然我來到正確的地方問這個問題! – 2008-11-24 14:05:07
請注意,陽光的答案比我的好,因爲他已經明白你想要什麼。無需像我向你展示的那樣重新綁定。只有在您使用A時才需要此功能,但偶爾會有需要或A 。或者如果用戶的視圖是A ,但是您使用A 。 –
2008-11-24 14:14:33