【发布时间】:2013-03-08 19:42:23
【问题描述】:
我遇到了一个 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 中的类型仍然未绑定。
【问题讨论】:
-
你想用这个做什么?也许有更好的方法。
-
这是你想要的吗? stacked-crooked.com/…
-
@Pubby 我在最后添加了一个带有动机的部分。
-
回复:您的编辑:那将是函数模板的部分专业化。不可能。您可以通过将函数委托给类模板来模拟它。或者普通的重载怎么样?
-
@TimothyShields 只是用
template<typename... T> void f(std::tuple<T...>);超载了吗?
标签: c++ templates c++11 template-specialization