wrapper.cpp

The following code example is taken from the book
C++ Templates - The Complete Guide, 2nd Edition
by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor,
Addison-Wesley, 2017
© Copyright David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor 2017


class S {
};

template<typename T> 
class Wrapper {
  private:
    T object;
  public:
    Wrapper(T obj) : object(obj) {  // implicit conversion from T to Wrapper<T>
    }
    friend void foo(Wrapper<T> const&) {
    }
};

int main() 
{
    S s;
    Wrapper<S> w(s);
    foo(w);  // OK: Wrapper<S> is a class associated with w
    foo(s);  // ERROR: Wrapper<S> is not associated with s
}