gpt4 book ai didi

c++ - std::tuple 作为模板参数?

转载 作者:太空狗 更新时间:2023-10-29 20:57:36 24 4
gpt4 key购买 nike

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

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

// 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
}
}

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

最佳答案

是的,这是可能的 - 您想要 Comparator 的部分特化:

template <typename T>
struct Comparator;

template <typename Col>
struct Comparator<std::tuple<Col, bool>>
{
// ...
};

关于c++ - std::tuple 作为模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30028228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com