我有以下类别:我如何专注/重载一个模板函数的模板类型
template <class... T>
class thing {};
template <class T>
class container {
public:
container() {
std::cout << "normal constructor" << std::endl;
}
};
我可以写的container<int>
以这种方式构造一个完整的专业化:
template <>
container<int>::container() {
std::cout << "int constructor" << std::endl;
}
我希望能够为container<thing<T>>
定义一个类似的构造函数。我想我试图写一个模板函数的偏特这里是我试图(这是非法的。):
template <class T>
container<thing<T>>::container() {
}
这并不编译。
我不完全确定解决这个问题的正确方法是什么,模板类函数的重载和专业化之间的界限变得模糊。这可以微不足道地解决,还是需要type_traits(std::enable_if
)?我该如何解决这个问题?
非常优雅的解决方案(恕我直言) – max66
@ max66谢谢。整个班级的部分专业化很快就会导致代码重复,如果可能的话,我通常会尽量避免它。 – skypjack
@ max66仅在多个_specialized_构造函数的情况下添加更多细节。我希望你也喜欢它。 ;-) – skypjack