Skip to content

Instantly share code, notes, and snippets.

@wancw
Created October 21, 2014 08:33
Show Gist options
  • Save wancw/01088dba0e44404912f5 to your computer and use it in GitHub Desktop.
Save wancw/01088dba0e44404912f5 to your computer and use it in GitHub Desktop.
Declare a method of class template as friend funciton
// Generic template
template <typename T>
struct Foo {
static int bar(const T& t) { return -1; }
};
// Specialization
class C;
template<>
class Foo<C> {
public:
static int bar(const C& c);
};
// Class for template parameter
class C {
public:
C(int i): i_(i) {}
friend int Foo<C>::bar(const C&);
private:
int i_;
};
// Implementation of template specialization
int Foo<C>::bar(const C& c) {
return c.i_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment