Search code examples
c++templatesgenericscomparable

C++ determine if class is comparable


I'm more or less Java programmer, so this might be a stupid question, but I didn't manage to find any simple solution.

I have a class like this in C++:

template<class T> class Node {...}

And I need T to be comparable - to have at least == < > operators defined. Is there any simple way to do this - or what is the best practice for this? In Java, it would be something like this:

public class Node<T extends Comparable> { ... }

Thanks for your help!


Solution

  • C++ templates are duck-typed, so no interface or constraint is necessary, the compiler will use the comparison operators if they exist, and generate an error if not.

    See also this more detailed answer.