Skip to Content
Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions
book

Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

by Herb Sutter
August 2004
Intermediate to advanced
352 pages
7h 38m
English
Addison-Wesley Professional
Content preview from Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

Chapter 8. Befriending Templates

Difficulty: 4

If you want to declare a function template specialization as a friend, how do you do it? According to the C++ standard, you can choose either of two legal syntaxes. According to real-world compilers, however, one of the syntaxes is widely unsupported; the other works on all current versions of popular compilers… except one.

Let's say we have a function template that does SomethingPrivate to the objects it operates on. In particular, consider the boost::checked_delete function template from [Boost], which deletes the object it's given—among other things, it invokes the object's destructor:

namespace boost {
 template<typename T> void checked_delete(T* x) {

  // … other stuff …

  delete x;
 }
}

Now, say you ...

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

Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

Herb Sutter
Navigating C++ and Object-Oriented Design

Navigating C++ and Object-Oriented Design

Paul Anderson, Gail Anderson

Publisher Resources

ISBN: 0201760428Purchase book