January 2019
Intermediate to advanced
512 pages
14h 5m
English
The pattern that we are about to see is a very old one, and was introduced by John Barton and Lee Nackman in 1994 for a completely different purpose—they used it to work around certain limitations of the compilers that existed at the time. The inventors proposed the name Restricted Template Expansion, which was never widely used. Years later, Dan Sacks coined the name Friends Factory, but the pattern is also sometimes referred to simply as the Barton-Nackman trick.
The pattern looks very simple and very similar to the code we wrote earlier, throughout this chapter:
template <typename T> class C { int x_; public: C(int x) : x_(x) {} friend C operator+(const C& lhs, const C& rhs) { return C(lhs.x_ + rhs.x_);