2

我有一堂课

template<class T, bool isOrdered>
class Vector
{
public:
    int Find(const T& t); // Return its index if found.

    // Many other methods.
};

Find取决于true或有两个版本falseisOrdered成员方法没有部分特化(class T不是特化的)。我的问题是如何专业化它们?谢谢。

4

1 回答 1

2

在 上使用重载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;}
};
于 2013-07-03T05:17:24.353 回答