我的一个类声明了一个模板化函数:
template<class A, class B>
A do_something(const std::vector<B> &data)
我想部分专注于typename A
. B
是一个实现了非常小的接口的类型系列,并且我们使用了很多它们,所以我希望我的专业化是通用的B
。我怀疑这是双重令人烦恼的typename A
仅用作返回类型。
从互联网上,我发现我不能部分专门化一个函数,所以我创建了一个类,如下所示:
template<class A, class B>
class do_something_implementation {
public:
do_something_implementation(const std::vector<B> &data_) {
data = data_;
}
int do_something_implementation<int, B>::operator()() {
/* Complicated algorithm goes here... */
}
double do_something_implementation<double, B>::operator()() {
/* Different complicated algorithm goes here... */
}
private:
std::vector<B> data;
}
当我尝试编译它(使用 Visual Studio 2008)时,编译器崩溃(!)并且出现以下错误:
fatal error C1001: An internal error has occurred in the compiler.
我认为这是我的问题而不是编译器的问题。有没有更好的方法来表达我所追求的部分专业化?
通常,它是这样的:
template <typename A, typename B>
struct DoSomethingHelper
{
static A doIt(const std::vector<B> &data);
};
template <typename B>
struct DoSomethingHelper<double, B>
{
static double doIt(const std::vector<B> &data) { ... }
};
template <typename B>
struct DoSomethingHelper<int, B>
{
static int doIt(const std::vector<B> &data) { ... }
};
template<class A, class B>
A do_something(const std::vector<B> &data)
{ return DoSomethingHelper<A, B>::doIt(data); }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)