std :: tuple作为模板参数?

Shm*_*opy 3 c++ templates c++11

我正在尝试编写一个std::sort模板比较类,该类应该接收未知数量的元组(可变模板)。每个元组都应由一列(我们的代码中有某种类型)和布尔值组成,并指定该列应按升序还是降序排序。

基本上,我想要类似的东西:

// doesn't compile - conceptual code
template <typename std::tuple<Col, bool>>
struct Comparator
{
    bool operator() (int lhs, int rhs)
    {
         // lhs and rhs are row indices. Depending on the columns
         // and the bools received, decide which index should come first
    } 
}
Run Code Online (Sandbox Code Playgroud)

在C ++ 11中这种事情可能吗?

Bar*_*rry 5

是的,有可能-您想要部分专业化Comparator:

template <typename T>
struct Comparator;

template <typename Col>
struct Comparator<std::tuple<Col, bool>>
{
    // ...
};
Run Code Online (Sandbox Code Playgroud)