我遇到了C++模板難題。我試圖把它縮小到最低限度,現在我甚至不確定我想要做什麼是可能的。看看下面的代碼(在一些.h文件中)。模板難題
template<typename T>
class A
{
public:
template<typename S>
void f(S x);
};
class B1 { };
template<typename S>
class B2 { };
//This one works:
template<>
template<typename S>
void A<B1>::f(S x)
{
}
//This one does not work:
template<>
template<typename S>
void A<B2<S>>::f(S x)
{
}
在我main
功能我有這樣的事情:
//This one works:
A<B1> first;
first.f<int>(5);
//This one does not work:
A<B2<int>> second;
second.f<int>(5);
錯誤消息我得到的,因爲第二部分是
error C3860: template argument list following class
template name must list parameters in the
order used in template parameter list
error C3855: 'A<T>': template parameter 'T' is
incompatible with the declaration
任何想法的問題是什麼?
編輯
爲了使問題更具體,這裏是我的動力。我希望上述功能f
針對T=std::tuple<T1, T2>
,T=std::tuple<T1, T2, T3>
和T=std::tuple<T1, T2, T3, T4>
進行專業化,其中tuple
中的類型仍未綁定。
你想用這個做什麼?也許有更好的方法。 – Pubby 2013-03-08 19:47:07
這是你想要的嗎? http://stacked-crooked.com/view?id=8b9365eab6441615b312ad76b0500bb7-18aa934a8d82d638dde2147aa94cac94 – Pubby 2013-03-08 19:53:28
@Pubby我在動機的最後加了一段。 – 2013-03-08 20:05:35