【问题标题】:c++ universal templated class arithmeticC++ 通用模板类算术
【发布时间】:2019-03-04 06:19:07
【问题描述】:

假设我们正在处理一个由于某种原因必须进行算术运算的类。

tensor_sum 这样的操作有重载的操作符模板。这种方法的问题似乎是这样的:

g++ main.cpp -o main
main.cpp: In instantiation of ‘tensor_sum<T0, T1>::value_type& 

tensor_sum<T0, T1>::operator()(unsigned int) const [with T0 = tensor<int>; T1 = tensor<int>; tensor_sum<T0, T1>::value_type = int; typename T0::value_type = int; typename T1::value_type = int]’:
main.cpp:46:20:   required from here
main.cpp:11:63: error: no match for call to ‘(const tensor<int>) (unsigned int&)’
   value_type & operator () (unsigned int i) const { return t0_(i) + t1_(i); }
                                                            ~~~^~~
main.cpp:32:7: note: candidate: T& tensor<T>::operator()(unsigned int) [with T = int] <near match>
   T & operator () (unsigned int i) { return values_[i]; }
       ^~~~~~~~
main.cpp:32:7: note:   passing ‘const tensor<int>*’ as ‘this’ argument discards qualifiers
main.cpp:11:72: error: no match for call to ‘(const tensor<int>) (unsigned int&)’
   value_type & operator () (unsigned int i) const { return t0_(i) + t1_(i); }
                                                                     ~~~^~~
main.cpp:32:7: note: candidate: T& tensor<T>::operator()(unsigned int) [with T = int] <near match>
   T & operator () (unsigned int i) { return values_[i]; }
       ^~~~~~~~
main.cpp:32:7: note:   passing ‘const tensor<int>*’ as ‘this’ argument discards qualifiers

由于某种原因,我无法访问该值。但是我已经超载了() operator

这里是代码:

#include <iostream>
#include <vector>

template<typename T0, typename T1>
struct tensor_sum {
  typedef decltype(typename T0::value_type() + typename T1::value_type()) value_type;

  public:
  tensor_sum(const T0 &t0, const T1 &t1) : t0_(t0), t1_(t1) {}

  value_type & operator () (unsigned int i) const { return t0_(i) + t1_(i); }

  private:
  const T0 &t0_;
  const T1 &t1_;
};

template<typename T0, typename T1>
tensor_sum<T0, T1> operator + (const T0 &t0, const T1 &t1) { return tensor_sum<T0, T1>(t0, t1); }

template<typename T0, typename T1>
tensor_sum<T0, T1> operator + (const T0 &t0, const T1 &t1);

template<typename T>
struct tensor {
  typedef T value_type;

  public:
  tensor(const unsigned int s = 0) : size_(s), values_(std::vector<T>(s)) {}
  tensor(const tensor<T> &t) : size_(t.size_), values_(std::vector<T>(t.values_)) {}

  T & operator () (unsigned int i) { return values_[i]; }
  tensor<T> & operator = (const tensor<T> &t) { return tensor<T>(t); }

  private:
  const unsigned int size_;
  std::vector<T> values_;
};

int main() {
  tensor<int> t0(10);
  tensor<int> t1(10);

  tensor_sum<tensor<int>, tensor<int>> ts = t0 + t1;

  std::cout << ts(2) << std::endl; //Can't access value.. why?

  return 0;
}

Live example

【问题讨论】:

  • 顺便说一句。这个operator + 可以在任何单一的两种类型上调用(包括例如int)。
  • 除了已经说过的,你要小心这个:“我会一遍又一遍地重用这些操作,所以我认为将它们全部存储在一个单独的命名空间。” 将它们“存储”在一个单独的命名空间意味着它们不会被ADL 找到,这对于重载运算符的正常工作来说有点重要......
  • 您的样本还有其他几个问题:tensor_sum 无法构造,tensor&lt;int&gt; 没有value_type,因此不能用作tensor_sum 的类型,operator = return by copy :/ 如果大小不匹配会崩溃,...
  • Yes operator = 如果类型不同会崩溃,所以我只需为等号运算符创建一个模板。主要问题是 value_type。如何在张量结构中声明 value_type?
  • 适当使用auto/decltype,您可能不需要value_type。但是对于tensor,它将是T

标签: c++ templates operators typedef decltype


【解决方案1】:
tensor_sum<T0, T1> operator + (const T0 &t0, const T1 &t1);

返回一个 tensor_sum 并且 tensor 没有 operator= 和 tensor_sum。

所以你的代码意味着 t0 + t1 返回一个 tensor_sum 并尝试分配给一个张量,当然会失败。

【讨论】:

  • 好吧..我有点错了。我根本不需要存储张量......我可以使用 tensor_sum 结构,并且从这个结构中我可以重载另一个运算符。我将更改代码。希望你有另一个想法,为什么我不能使用 () 运算符访问 tensor_sum。
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2016-03-27
  • 2019-07-29
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2018-10-16
相关资源
最近更新 更多