Re: name of template friend function in template class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 28 March 2011 02:05, burlen.loring wrote:
> 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;
> }

I think G++ is correct, to make the friend declaration compile you need to say

template<typename U>
  friend void shift(Box<U> &b, int i);



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux