- Barton-Nackman trick
Barton-Nackman trick is a term coined by the C++ standardization committee (ISO/IEC JTC1 SC22 WG21) to refer to an idiom introduced by John Barton and Lee Nackman as "Restricted Template Expansion" [cite book | last=Barton | first=John J. | coauthors=Lee R. Nackman | title=Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples | publisher=Addison-Wesley Professional | year=1994 | id=ISBN 0-201-53393-6] .
The idiom
The idiom is characterized by an in-class friend function definition appearing in the base class template component of the
Curiously Recurring Template Pattern (CRTP).When a class template like
equal_comparable
is instantiated, the in-class friend definitions produce nontemplate (and nonmember) functions (operator functions in this case). At the time the idiom was introduced (1994) the C++ language didn't define a partial ordering for overloaded function templates and as a result overloading function templates often resulted in ambiguities. For example, trying to capture a generic definition foroperator=
aswould essentially be incompatible with another definition like
The Barton-Nackman trick, then, achieves the goal of providing a generic user-defined equality operator without having to deal with such ambiguities. The adjective "restricted" in the idiom name refers to the fact that the provided in-class function definition is restricted (only applies) to specializations of the given class template.
The term is sometimes mistakenly used to refer to the
Curiously Recurring Template Pattern (CRTP). As explained above, the Barton-Nackman trick is a distinct idiom (that relies on the CRTP).How It Works
When the compiler encounters the expression
v1 = v2
where
v1
andv2
are of typevalue_type
, it attemptsargument dependent lookup (ADL) foroperator=
. This lookup includes consideration of friend functions declared invalue_type
and its base classes. (Note that ifvalue_type
were an incomplete template instance, ADL would trigger its complete instantiation.)The Barton-Nackman trick originally relied not on ADL but on a (now obsolete) C++ feature called "friend name injection", in which an in-class declaration of a friend function made the function name visible in the immediately surrounding namespace scope (possibly the global scope). When investigating the possibility of removing friend name injection from the C++ programming language, Barton and Nackman's idiom was found to be the only reasonable use of that language rule. Eventually, the rules for argument-dependent lookup were adjusted [cite web | title=An Alternative to Name Injection from Templates | work=An Alternative to Name Injection from Templates | url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1995/N0777.pdf | accessdate=April 12 | accessyear=2005] to replace friend name injection by a less drastic mechanism, described above, that maintained the validity of Barton and Nackman's technique. It is worth noting that, as a consequence of this change, the expression
::operator=(v1,v2)
is no longer valid, because qualified names aren't subject to ADL and friend declarations aren't found via ordinary lookup. Note that this implies that the
friend
specifier is essential, even if the defined friend functions do not actually need to access nonpublic members of the befriending class.References
Further reading
*cite book | last=Vandevoorde | first=David | coauthors=Nicolai M. Josuttis | title=C++ Templates: The Complete Guide | publisher=Addison-Wesley Professional | year=2002 | id=ISBN 0-201-73484-2
Wikimedia Foundation. 2010.