This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]