如何在C++中执行元组运算(c ++ 11/c ++ 17)?

tin*_*lyx 5 c++ variadic-templates c++11 stdtuple c++17

我正在尝试编写模板函数/运算符,例如+在两个相同类型的元组之间进行算术运算.例如,对于

std::tuple<int,double> t = std::make_tuple(1,2);
Run Code Online (Sandbox Code Playgroud)

我想做

auto t1 = t + t;  
Run Code Online (Sandbox Code Playgroud)

逻辑很简单:按顺序进行算术运算.但我无法弄清楚如何在c ++模板编程中完成这项工作(c ++ 11/17).我下面的代码不能编译g++ -std=c++11 tuple_arith.cpp.特别是,我无法弄清楚使用泛型add函数(template<typename T> T add(T x, T y) { return x + y; })来处理元组操作代码的正确方法.

有人可以帮助解释如何解决问题吗?

#include <tuple>

namespace std {
  template<typename _Tp, size_t __i, size_t __size, typename _opT >
     struct __tuple_arith {
       static constexpr _Tp  __op(const _Tp& __t, const _Tp& __u, const _opT& op)  {
         return std::tuple_cat(std::make_tuple(op(std::get<__i>(__t), std::get<__i>(__u))
                               , __tuple_arith<_Tp, __i + 1, __size, _opT>::__op(__t, __u)));
       }
     };

  template<typename _Tp, size_t __size, typename _opT>
  struct __tuple_arith<_Tp, __size, __size - 1, _opT> {
       static constexpr _Tp __op(const _Tp& __t, const _Tp& __u, const _opT& op) {
         return std::make_tuple(op(std::get<__size-1>(__t), std::get<__size -1>(__u)));
       }
  };

  template<typename T> T add(T x, T y) { return x + y; }

  template<typename... _TElements> constexpr tuple<_TElements...>
  operator+(const tuple<_TElements...>& __t, const tuple<_TElements...>& __u) {
    using op = __tuple_arith<tuple<_TElements...>, 0, sizeof...(_TElements), decltype(add)>;
    return op::__op(__t, __u, add);
  }
}; //namespace std

#include <iostream>
using namespace std;

int main() {
  std::tuple<int,double> t = std::make_tuple(1,2);
  auto t1 = t + t;
  cout << std::get<0>(t1) << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

具体错误是:

tuple_arith.cpp:14:10: error: template argument ‘(__size - 1)’ involves template parameter(s)
   struct __tuple_arith<_Tp, __size, __size - 1, _opT> {
          ^
tuple_arith.cpp: In function ‘constexpr std::tuple<_Elements ...> std::operator+(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)’:
tuple_arith.cpp:24:90: error: decltype cannot resolve address of overloaded function
  __tuple_arith<tuple<_TElements...>, 0, sizeof...(_TElements), decltype(add)>;
                                                                            ^
tuple_arith.cpp:24:91: error: template argument 4 is invalid
  __tuple_arith<tuple<_TElements...>, 0, sizeof...(_TElements), decltype(add)>;
                                                                             ^
tuple_arith.cpp:25:12: error: ‘op’ has not been declared
     return op::__op(__t, __u, add);
            ^
tuple_arith.cpp: In instantiation of ‘constexpr std::tuple<_Elements ...> std::operator+(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&) [with _TElements = {int, double}]’:
tuple_arith.cpp:34:17:   required from here
tuple_arith.cpp:26:3: error: body of constexpr function ‘constexpr std::tuple<_Elements ...> std::operator+(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&) [with _TElements = {int, double}]’ not a return-statement
   }
   ^
Run Code Online (Sandbox Code Playgroud)

- 更新 -

感谢目前为止提供的有用答案.是否可以使其适用于任何操作员包装,例如std::{plus,minus,multiplies,divides}?这就是我试图用模板参数实现的typename _opT.最后,我正在寻找一个可以将兼容的运算符作为参数的函数/对象.

max*_*x66 4

代码中的问题是,您无法基于另一个模板值部分特化一个模板值;你可以解决这个问题但是...为什么呢?

得到你想要的不是那么简单std::index_sequence

#include <tuple>
#include <iostream>

template <typename ... Ts, std::size_t ... Is>
std::tuple<Ts...> sumT (std::tuple<Ts...> const & t1,
                        std::tuple<Ts...> const & t2,
                        std::index_sequence<Is...> const &)
 { return { (std::get<Is>(t1) + std::get<Is>(t2))... }; }

template <typename ... Ts>
std::tuple<Ts...> operator+ (std::tuple<Ts...> const & t1,
                             std::tuple<Ts...> const & t2)
 { return sumT(t1, t2, std::make_index_sequence<sizeof...(Ts)>{}); }

int main ()
 {
   std::tuple<int,double> t = std::make_tuple(1,2);
   auto t1 = t + t;
   std::cout << std::get<0>(t1) << std::endl;
   std::cout << std::get<1>(t1) << std::endl;
 }
Run Code Online (Sandbox Code Playgroud)

无论如何......我认为向标准类型添加运算符不是一个好主意;也许你只能定义一个sumT()函数。

PS:std::index_sequencestd::make_index_sequence是c++14/17特性;但在 c++11 中模拟它们并不太复杂。

- 编辑 -

OP问

非常感谢,是否可以使此功能适用于任何操作员包装器?请查看更新内容

我想你的意思如下

#include <tuple>
#include <iostream>
#include <functional>

template <typename Op, typename Tp, std::size_t ... Is>
auto opH2 (Op const & op, Tp const & t1, Tp const & t2,
           std::index_sequence<Is...> const &)
 { return std::make_tuple( op(std::get<Is>(t1), std::get<Is>(t2))... ); }

template <typename Op, typename Tp>
auto opH1 (Op const & op, Tp const & t1, Tp const & t2)
 { return opH2(op, t1, t2,
               std::make_index_sequence<std::tuple_size<Tp>{}>{}); }

template <typename ... Ts>
auto operator+ (std::tuple<Ts...> const & t1, std::tuple<Ts...> const & t2)
 { return opH1(std::plus<>{}, t1, t2); }

template <typename ... Ts>
auto operator- (std::tuple<Ts...> const & t1, std::tuple<Ts...> const & t2)
 { return opH1(std::minus<>{}, t1, t2); }

template <typename ... Ts>
auto operator* (std::tuple<Ts...> const & t1, std::tuple<Ts...> const & t2)
 { return opH1(std::multiplies<>{}, t1, t2); }

template <typename ... Ts>
auto operator/ (std::tuple<Ts...> const & t1, std::tuple<Ts...> const & t2)
 { return opH1(std::divides<>{}, t1, t2); }

int main ()
 {
   std::tuple<int,double> t = std::make_tuple(1,2);

   auto t1 = t + t;
   auto t2 = t - t;
   auto t3 = t * t;
   auto t4 = t / t;

   std::cout << std::get<0>(t1) << ", " << std::get<1>(t1) << std::endl;
   std::cout << std::get<0>(t2) << ", " << std::get<1>(t2) << std::endl;
   std::cout << std::get<0>(t3) << ", " << std::get<1>(t3) << std::endl;
   std::cout << std::get<0>(t4) << ", " << std::get<1>(t4) << std::endl;
 }
Run Code Online (Sandbox Code Playgroud)