[Bug c++/17122] Unable to compile friend operator within template

manu at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Apr 12 13:06:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17122

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu.org

--- Comment #14 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-04-12 13:05:16 UTC ---
(In reply to comment #13)
> Are any of the examples in this PR actually valid?
> 
> I think G++ is correct to reject them.
> 
> foo ::operator+ is parsed the same as foo::operator+ so still finds the member
> function, the parentheses prevent that.

Maybe g++ is correct to reject them, but the diagnostics printed are awful.
They are actually worse than saying:

error: Something is wrong somewhere, Please fix it.

template <class blah>                                                           
class foo;                                                                      

template <class blah>                                                           
void operator+(int, foo<blah>);                                                 

template <class blah>                                                           
void operator-(int, foo<blah>);                                                 

template <class blah>                                                           
class foo                                                                       
{                                                                               
public:                                                                         
  void operator+(int){}                                                         
  friend void operator+<>(int, foo);//eliminating <> generates warning to add 
                                    //them                                      
  friend void operator-(int, foo);//eliminating <> generates warning to add 
                                    //them                                      

};                                                                              
template <class blah>                                                           
void operator+(int, foo<blah>){}                                                

template <class blah>                                                           
void operator-(int, foo<blah>){}                                                

int main()                                                                      
{                                                                               
  foo<double> inst;                                                             
  inst+1;                                                                       
  1+inst;                                                                       
};

pr17122.C:15:23: error: declaration of ‘operator+’ as non-function
   friend void operator+<>(int, foo);//eliminating <> generates warning to add 
                       ^
pr17122.C:15:23: error: expected ‘;’ at end of member declaration
   friend void operator+<>(int, foo);//eliminating <> generates warning to add 
                       ^
pr17122.C:15:24: error: expected unqualified-id before ‘<’ token
   friend void operator+<>(int, foo);//eliminating <> generates warning to add 
                        ^
pr17122.C:17:33: warning: friend declaration ‘void operator-(int, foo<blah>)’
declares a non-template function [-Wnon-template-friend]
   friend void operator-(int, foo);//eliminating <> generates warning to add 
                                 ^
pr17122.C:17:33: note: (if this is not what you intended, make sure the
function template has already been declared and add <> after the function name
here) 
   friend void operator-(int, foo);//eliminating <> generates warning to add 
                                 ^



More information about the Gcc-bugs mailing list