重载元组索引运算符-C++

Overload tuple indexing operator - C++

本文关键字:-C++ 运算符 索引 元组 重载      更新时间:2023-10-16

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

如其他答案中所述,不可能像std::tuple那样向std类型添加任何成员函数。并且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]);
}

这是不可能的,原因有二:

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

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