我的一个类声明了一个模板函数:
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.
我认为这是我的问题,而不是编译器的问题。有没有更好的方法来表达我想要的部分专业化?