【问题标题】:Overload tuple indexing operator - C++重载元组索引运算符 - C++
【发布时间】:2020-07-11 18:01:22
【问题描述】:

如何为std::tuple<int,int,int> 重载索引[] 运算符?所以当我有std::tuple<int,int,int> tup 并输入tup[0] 时,我希望它返回对get<0>(tup) 的引用。这可能吗?

【问题讨论】:

  • 为什么不改用std::array<int,3>
  • 您好!你能详细说明你为什么要这样做吗?如果您需要在运行时按索引查找元组元素,您可能不想使用元组。
  • 我只是想知道这是否可能,我知道重载输入和输出运算符,但想知道索引运算符重载。

标签: c++ indexing tuples operator-overloading std


【解决方案1】:

正如其他答案中所述 - 无法将任何成员函数添加到 std 类型,例如 std::tuple。而operator[] 必须是非静态成员函数。

但是你可以包装这个元组 - 并将 operator[] 添加到这个包装器类型。在这种情况下 - 您需要知道元组的所有元素的通用返回类型。嗯 - 有 std::any 可以适用于大多数类型。

这应该可行,但这只是为了满足您的好奇心——在真实软件中使用类似的东西是糟糕的设计:

template <typename Tuple, typename ReturnType = std::any>
class TupleExtractor
{
public:
    TupleExtractor(const Tuple& tuple) 
        : TupleExtractor(tuple, std::make_index_sequence<std::tuple_size_v<Tuple>>{})
    {}
    
    ReturnType operator[](std::size_t index) const
    {
        return extractors[index](tuple);
    }

private:
    template <std::size_t I>
    static ReturnType get(const Tuple& tuple)
    {
        return std::get<I>(tuple);
    }


    template <std::size_t ...I>
    TupleExtractor(const Tuple& tuple, std::index_sequence<I...>) 
        : tuple(tuple), 
          extractors{&TupleExtractor::get<I>...}
    {}

    const Tuple& tuple;
    using Extractor = std::any(*)(const Tuple&);
    std::vector<Extractor> extractors;
};

并测试 - 它是否有效:

int main() {
    std::tuple<int, int, int> a{1,2,3};
    TupleExtractor e{a};
    
    return std::any_cast<int>(e[2]);
}

【讨论】:

    【解决方案2】:

    这不可能有两个原因:

    1. operator[] 必须是非静态成员函数,并且由于您没有实现标准库,因此无法将成员函数添加到 std::tuple

    2. 索引必须是一个常量表达式,不能用函数参数强制执行。

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 1970-01-01
      • 2021-09-17
      • 2023-03-29
      • 2019-08-21
      • 2012-03-24
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      相关资源
      最近更新 更多