Group: comp.lang.c++
From: saneman
Date: Friday, February 15, 2008 3:04 PM
Subject: Re: Operator as non-function

Victor Bazarov wrote:
> saneman wrote:
>> But if I try to overload the + operator I get the same error:
>>
>>
>>
>> template< class A > class BOB;
>> template< class A >
>> BOB operator+(typename BOB::difference_type, BOB const& );
>>
>>
>> template
>> class BOB {
>> private:
>> typedef BOB
iterator;
>> public:
>> typedef typename A::difference_type difference_type;
>> friend iterator operator + <> (difference_type i, const
>> iterator& it);
>>
>> // Added overload of + operator
>> iterator operator + (const difference_type i) const {
>> return iterator(1);
>> }
>>
>> };
>>
>>
>> When Compiling I get:
>>
>> error: declaration of ‘operator+’ as non-function
>> error: expected ‘;’ before ‘<’ token
>>
>> Where the error refers to:
>>
>>
>> friend iterator operator + <> (difference_type i, const
>> iterator& it);
>>
>>
>> Does that mean that overloading of template operators are impossible?
>
> Take your code to another compiler and see how they react. One of
> the most compliant when it comes to templates is Comeau C++. They
> have the online trial version. Use it. See what it says. I would
> do it myself, but you didn't post the entire code, did you? If I
> take what you posted, Comeau compiles it OK.
>
> V


I have now tried it on 3 different machines with the same error. The
test program consist of test.h I compile with:

g++ test.h

And the error on all machines are:

test.h:16: error: declaration of 'operator+' as non-function
test.h:16: error: expected ';' before '<' token


Below are the exact content of test.h:

template class BOB;
template
BOB
operator + (typename BOB::difference_type, BOB const&);

template
class BOB {
private:
typedef BOB
iterator;
public:
typedef typename A::difference_type difference_type;

iterator operator + (const difference_type i) const {
return iterator(1);
}

friend iterator operator + <> (difference_type i, const iterator& it);
};


But maybe this kind of code is only meant to run with Comeau.


Safety Articles | Usenet Groups | Usenet News | Bluegrass