More C++ Idioms/Barton-Nackman trick

= Barton-Nackman trick =

Intent
Support overloaded operators without relying on namespaces or function template overload resolution.

Also Known As
The inventors originally referred to it as Restricted Template Expansion, though this term has never been widely used.

Motivation
John Barton and Lee Nackman first published this idiom in 1994 in order to work around limitations of the C++ implementations available at the time. Though it is no longer necessary for its original purpose, the current standard retains support for it.

At the time Barton and Nackman originally developed the idiom, C++ did not support overloading of function templates and many implementations still didn't support namespaces. This caused problems when defining operator overloads for class templates. Consider the following class:

The most natural way to define the equality operator is as a non-member function at namespace scope (and since compilers at the time didn't support namespaces, therefore at global scope). Defining  as a non-member function means that the two arguments are treated symmetrically, which doesn't happen if one argument is a   pointer to the object. Such an equality operator might look like this:

However, since function templates couldn't be overloaded at the time, and since putting the function in its own namespace wouldn't work on all platforms, this would mean that only one class could have such an equality operator. Doing the same thing for a second type would cause an ambiguity.

Solution and Sample Code
The solution works by defining an operator in the class as a friend function:

Instantiating the template now causes a non-template function to be injected into global scope with the argument types being concrete, fixed types. This non-template function can be selected through function overload resolution the same way as any other non-template function.

The implementation can be generalised by providing the friend functions as part of a base class that is inherited from via the Curiously Recurring Template Pattern:

Known Uses

 * Boost.Operators library

Related Idioms

 * Curiously Recurring Template Pattern