I have an overload set of a commutative binary function called overlap that takes two different types:
class A a; class B b; bool overlap(A, B); bool overlap(B, A);
My overlap function returns true if and only if one form overlaps another - this is one common example used when discussing multimethods .
Since overlap(a, b) equivalent to overlap(b, a) , I only need to implement one βsideβ of the relationship. One recurring solution is to write something like this:
bool overlap(A a, B b) { } bool overlap(B b, A a) { return overlap(a, b); }
But I would rather not write extra N! / 2 N! / 2 trivial versions of the same function, allowing them to be generated instead using the template.
template <typename T, typename U> bool overlap(T&& t, U&& u) { return overlap(std::forward<U>(u), std::forward<T>(t)); }
Unfortunately, this is subject to infinite recursion, which is unacceptable: see http://coliru.stacked-crooked.com/a/20851835593bd557
How can I prevent such infinite recursion? Did I get the problem right?
c ++ templates metaprogramming
Max Bozzi Jul 06 '17 at 8:16 2017-07-06 08:16
source share