1

Let's suppose we are dealing with a class that for some reason has to have some arithmetic operations.

The operations like tensor_sum have overloaded operator templates. The problem with this approach seems to be this:

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

For some reason, I can't access the value. But I have overloaded the () operator

Anyway here is the code:

#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

5
  • 1
    btw. this operator + may be called on any single 2 types (including e.g. int). Commented Sep 28, 2018 at 9:29
  • 1
    In addition to what has already been said, you wanna be careful with this: "I would reuse those operations over and over again, so I thought it would be simpler to store them all in a separate namespace." "Storing" them in a separate namespace means that they will not be found by ADL, which is kinda important for overloaded operators to work properly… Commented Sep 28, 2018 at 9:35
  • 1
    There are several other issues with your sample: tensor_sum cannot be constructed, tensor<int> doesn't have value_type, so cannot be used as type of tensor_sum, operator = return by copy :/ would crash if size mismatch, ... Commented Sep 28, 2018 at 9:50
  • Yes operator = would crash if types differ, so I would just create a template for the equal operator. The main problem is value_type. How can i declare value_type in the tensor struct? Commented Sep 28, 2018 at 10:02
  • with appropriate usage of auto/decltype, you probably don't need value_type. But for tensor, it would be T. Commented Sep 28, 2018 at 10:35

1 Answer 1

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

return a tensor_sum and tensor has not a operator= with tensor_sum.

so you code means t0 + t1 return a tensor_sum and try to assign to a tensor, and fail of course.

Sign up to request clarification or add additional context in comments.

1 Comment

okay.. i was a bit wrong. I dont have to store the tensor at all... i can just use the tensor_sum struct and from this struct i can overload another operator. I'll change the code. Hopefully u have another idea why i cant access the tensor_sum with the () operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.