Question: When creating a function template in C++, is it possible to specify something like a generic constraint in C# on a type parameter? For instance this function template makes sense only for types that define `<` and `==` (in C# the signature would be `int Compare<T>(T item1, T item2) where T : IComparable<T>`):

```
template <typename T>
int compare(const T &item1, const T &item2)
{
    if (item1 == item2) return 0;
    if (item1 < item2) return -1;
    if (item2 < item1) return 1;
}

```

Answer: 
Yes, that's possible, but it's not necessarily desirable or required.

If your types don't provide the required operators, you'll get a compile-time error anyway, saying something to the effect that you can't apply the operator to those types. So there's no outright *danger* in having an unconstrained template.

It is possible, using metaprogramming techniques such as SFINAE, to remove the template from the overload set entirely unless the types meet the constraints, so that the template definition will never even be considered, but that will potentially lead to a weirder situation and error message. It is sometimes desirable or necessary to remove overloads from consideration (usually for constructors), but this doesn't appear to be such a case. Moreover, the techniques for doing this are a bit verbose and ad-hoc.

A future version of C++ may try and solve this via *concepts*, but that is not a mature technology yet.
It's possible by using boost, but not possible in current standard. There will be concepts lite (in feature) ([read this for more information](https://isocpp.org/blog/2013/02/concepts-lite-constraining-templates-with-predicates-andrew-sutton-bjarne-s)). Now you can only write your own library, use `std::enable_if` or something else, or use [boost concept check library](http://www.boost.org/doc/libs/1_55_0/libs/concept_check/concept_check.htm).