This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
name of template friend function in template class
- From: "." <burlen dot loring at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sun, 27 Mar 2011 18:05:11 -0700
- Subject: name of template friend function in template class
Hi,
I'm having some trouble with a template friend function in a template
class. If I have a member function with the same name it won't compile,
is this a bug in g++, or my code ?
for example, the function shift in the following.
test2.cpp:18: error: variable or field ‘shift’ declared void
test2.cpp:18: error: expected ‘;’ before ‘<’ token
changing the name of the friend function fixes it, but I'd like to have
the same name for both the friend and member function.
template<typename T>
class Box;
template<typename T>
void shift(Box<T> &b, int i)
{
b.v += i;
}
template<typename T>
class Box
{
public:
void shift(int i);
private:
friend void shift<>(Box<T> &b, int i);
private:
T v;
};
template<typename T>
void Box<T>::shift(int i)
{
v += i;
}
int main(int argc, char **argv)
{
Box<int> b;
b.shift(1);
shift(b,1);
return 0;
}