0

This will not work

template<typename T>
struct foo {
  T t;
};
bool operator==(const foo &lhs, const foo &rhs) { //error, requires template arg
  return lhs.t == rhs.t;
}

Is this the correct way to solve this? I want define also the operators <,>,<=,>=,!= so doing template<typename T> on all of them would be lengthy.

template<typename T>
struct foo {
  T t;
};
template<typename T>
bool operator==(const foo<T> &lhs, const foo<T> &rhs) {
  return lhs.t == rhs.t;
}
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
7cows
  • 4,974
  • 6
  • 25
  • 30

2 Answers2

2

There are two solutions: you can define them as const member functions inside the class

template<typename T>
struct foo {
  T t;

  bool operator==(const foo &lhs, const foo &rhs) const { return lhs.t == rhs.t; }
  // same for the other relational operators
};

This works because inside the class you can use foo as a shorthand for foo<T>.

An alternative is to define them as friend non-member functions inside the class

template<typename T>
class foo {
  T t;

  friend bool operator==(const foo &lhs, const foo &rhs) const { return lhs.t == rhs.t; }
  // same for the other relational operators
};

If you define t as a private member, then you actually need to make operator== a friend function in order to let it gain access. Note however, that this will have the side-effect as injecting them as non-member non-template functions in the surrounding namespace. This has some consequences for argument-dependent name lookup.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
0

if you don't care about implicit conversions, you can set them as member functions, so you won't have to retype it each time.

but if you have to define them as free functions, I'm afraid you don't have a choice.

Julien Lopez
  • 1,021
  • 6
  • 14
  • When should one prefer to do these operators as members over non-members? I never quite got that. – 7cows Jun 10 '13 at 10:20
  • @7cows you should make them non-members when you want to allow for implicit conversions. This has nothing to do with templates though. – juanchopanza Jun 10 '13 at 11:00