【发布时间】:2017-02-17 23:24:04
【问题描述】:
我有以下课程:
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)?我该如何解决这个问题?
【问题讨论】:
标签: c++ templates c++14 template-specialization partial-specialization