我有一堂课
template<class T, bool isOrdered>
class Vector
{
public:
int Find(const T& t); // Return its index if found.
// Many other methods.
};
Find
取决于true
或有两个版本false
。isOrdered
成员方法没有部分特化(class T
不是特化的)。我的问题是如何专业化它们?谢谢。
在 上使用重载std::integral_constant
:
template<class T, bool isOrdered>
struct Vector {
int find(const T& t) {
return find_impl(t, std::integral_constant<bool,isOrdered>());
}
int find_impl (const T& t, std::true_type) {return 1;}
int find_impl (const T& t, std::false_type) {return 2;}
};