是否可以对具有不同非类型模板参数值的模板类进行“部分专业化”? 更具体地说,我尝试根据不同的非类型模板参数将 std :: vector 和 std :: array 的使用组合到一个类中,如下所示:
template<typename T, int Size> //if Size given, use std::array stuff
class MyArray:public std::array<T, Size> {...}
template<typename T> //if Size is not given, use std::vector stuff
class MyArray:public std::vector<T> {...}
但是第二个模板将是重新定义的模板错误。我尝试使用 std :: enable_if ,但不知道如何在此处正确使用它。
答案 0 :(得分:2)
您可以使用前哨值:
template<typename T, int Size = -1> //if Size given, use std::array stuff
class MyArray:public std::array<T, Size> {};
template<typename T> //if Size is not given, use std::vector stuff
class MyArray<T, -1>:public std::vector<T> {};