This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: non template friend function and overloading
- From: Martin Sebor <msebor at gmail dot com>
- To: Shmuel Hanoch <shmuel at infinitylabs dot co dot il>, gcc-help at gcc dot gnu dot org
- Date: Sun, 26 Mar 2017 15:22:40 -0600
- Subject: Re: non template friend function and overloading
- Authentication-results: sourceware.org; auth=none
- References: <CAMmv=H_J4aKkhGTuDVyTxazP6=n7NF3cWAHnsW3H=NE0k9i+Xg@mail.gmail.com>
On 03/26/2017 12:56 PM, Shmuel Hanoch wrote:
Hi,
I am trying to declare a (non template) friend function inside a template class.
This code compiles just fine:
#include <iostream>
template <class T>
struct Foo;
template <class T>
std::istream& operator>> (std::istream& is, Foo<T>& x);
template<class T>
struct Foo
{
//Foo operator>>(int x) const;
friend std::istream& operator>> <>(std::istream& is, Foo<T>& x);
};
But if I remove the comment from the other overload, I get this error message:
error: declaration of ‘operator>>’ as non-function
Is this overload illegal?
A declaration of an explicit specialization requires that
a declaration of the corresponding primary template be in scope.
In your example, when looking for the primary template, GCC first
finds the member operator with the same name. Because the member
operator is not a template, GCC issues an error (unfortunately,
the error doesn't make this clear.) The error can be avoided by
declaring the non-template after the friend.
But as some else has noted, unless you suspect that the error is
due to a GCC bug (as opposed to a misuse of the language) this
isn't the right place for general questions about C++.
Martin