Hey,

I have a set of types which are all arithmetic (have operators for addition, subtraction, multiplication, etc.). I want to aggregate any number of them into a single object, so I'm using the std::tuple class template and the <tuple> library (and Boost.Tuple for backward compatibility).

My question is a bit of a shot in the dark: Has anyone heard or seen a tuple-like class template that implements all the arithmetic operators?

For example, the std::tuple and boost::tuple classes both implement all the comparison operators which will only compile correctly if all the types contained in the tuple also have the comparison operators in question. I am wondering why there aren't the same provisions for the arithmetic operators?

I could easily implement this myself (but it can amount to a bit of trouble especially for backward compatibility with C++03), but I'm just wondering if anyone heard of one "arithmetic tuple" template that exists already.

Thanks for the reply vijayan, but my question is about tuples of heterogeneous types, not vector-like classes like vararray or TinyVector or many others of the kind.

Here is a more specific example of my problem. Say, I have a bunch of "state-space system" types each with some typedef for the type that represents the state-vector (doesn't have to be a vector), but they are all arithmetic (have + - -= += operators and a few more, like scalar multiplication). Say I have this code (very simplified):

template <typename SystemType>
struct system_traits {
  //..
  typedef typename SystemType::state_diff_type state_diff_type;
  //..
};


//Now, I want to combine many systems together, and for that I need to make composite state descriptors that are also arithmetic:
template <typename... Systems>
struct composite_system {
  //..
  typedef std::tuple< typename system_traits<Systems>::state_diff_type... > state_diff_type;
  //..
};

//Now, I have a concept-check class:
template <typename System>
struct StateSpaceSystemConcept {
  //..
  typename system_traits< System >::state_diff_type dx;

  BOOST_CONCEPT_USAGE(StateSpaceSystemConcept)
  {
    //..
    dx = dx + dx; 
    dx += dx;
    dx -= dx; 
    //.. etc..
  };
};

int main() {
  BOOST_CONCEPT_ASSERT((StateSpaceSystemConcept< composite_system< SystemA, SystemB, SystemA> >));
  //   ERROR: no operator+ for type std::tuple<..> 
  //    .. and so on for other operators.
};

As I said, I can pretty easily write a wrapper for the tuple type to add all those operators, but I was wondering if that didn't exist already and for that matter, why it is not in the std::tuple or boost::tuples::tuple class since other operators are already defined (comparison).

> why it is not in the std::tuple or boost::tuples::tuple class since other operators are already defined (comparison).

IIRC, there was a suggestion to overload the + operator for std::tuple<>. Not for element by element arithmetic plus, but for concatenation of tuples a la std::string.

There would be a similar ambiguity in the case of operator- (element by element minus or symmetric difference?), operator* ( element by element multiply, vector cross product or scalar dot product?).

IMHO, it was best that none of these operators were overloaded by the standard.

> why it is not in the std::tuple or boost::tuples::tuple class since other operators are already defined (comparison).

IIRC, there was a suggestion to overload the + operator for std::tuple<>. Not for element by element arithmetic plus, but for concatenation of tuples a la std::string.

There would be a similar ambiguity in the case of operator- (element by element minus or symmetric difference?), operator* ( element by element multiply, vector cross product or scalar dot product?).

IMHO, it was best that none of these operators were overloaded by the standard.

That makes sense. I understand the ambiguity.

I ended up implementing my own. If anyone is interested or find his way to this thread looking for an arithmetic-tuple class template, I have attached it to this post. My implementation is compatible with both C++11 and C++03 (using Boost Tuples), and I provide it, as is, with no guarantee, of course. Any review or comments on it is welcomed, of course.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.