Created
October 21, 2014 08:33
-
-
Save wancw/01088dba0e44404912f5 to your computer and use it in GitHub Desktop.
Declare a method of class template as friend funciton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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