如何“部分”专门化成员方法?

时间:2013-07-03 04:11:03

标签: c++ templates

我有一个班级

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

    // Many other methods.
};

Find有两个版本,具体取决于true的{​​{1}}或false。成员方法没有部分专门化(isOrdered不是专门的)。我的问题是如何专注于他们?感谢。

1 个答案:

答案 0 :(得分: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;}
};
相关问题