Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

Generating friends on demand

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_); 
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content