O'Reilly logo

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

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required