Element-wise tuple addition

前端 未结 4 838
后悔当初
后悔当初 2021-02-06 23:29

I have some values held in a tuple, and I am looking to add another tuple to it element-wise. So I would like functionality like this:

std::tuple         


        
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-06 23:45

    Here is an operator definition for syntax #1:

    template  std::tuple operator + (const std::tuple& lhs, const std::tuple& rhs)
    {
       return std::make_tuple(std::get<0>(lhs) + std::get<0>(rhs), std::get<1>(lhs) + std::get<1>(rhs));
    }
    

    Syntax #2 and #3 are not possible without creating a custom struct, because they can only be defined as members of the classes they operate on (and you can't touch existing classes in namespace std).

提交回复
热议问题