name of template friend function in template class

. burlen.loring@gmail.com
Mon Mar 28 10:23:00 GMT 2011


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;
}



More information about the Gcc-help mailing list