Topic: Does operator+= have to be implemented as a member?


Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Fri, 30 Jul 2004 16:26:15 GMT
Raw View
Forgive my ignorance in this matter.  Need clarification on this
simple issue:

   In 5.17 all operators that have the form @= where '@' is either
   empty or one of [ + - << >> * % / & | ^ ] are called "assignment
   operators".  The operator= form is called "simple assignment".
   A special case of the simple assignment is also called "copy
   assignment".  Correct so far?

   In 13.5.3 the Standard says "An assignment operator shall be
   implemented by a non-static member function with exactly one
   parameter".  Then, "Any assignment operator [..] can be virtual"

It seems that there is no distinction between operator= and other
operator@= in the Standard WRT how they should be overloaded for
classes.  Still, it seems that this code

    struct A {};
    A& operator +=(A&, A const&);
    int main() {
       A a, b;
       a += b;
    }

is not reported as ill-formed.

Have I missed a defect report somewhere?  Has it been already
clarified that operators += are not _really_ assignment when it
comes to overloading and are allowed to be implemented as non-
member functions?  Or should it have been clear to me because all
subclauses introducing restrictions to operator= do in fact mention
operator= explicitly and that means that other assignment operators
are not limited by those restrictions?

Thanks.

Victor

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Fri, 30 Jul 2004 17:06:48 GMT
Raw View
Victor Bazarov wrote:

> Forgive my ignorance in this matter.  Need clarification on this
> simple issue:
>
>   In 5.17 all operators that have the form @= where '@' is either
>   empty or one of [ + - << >> * % / & | ^ ] are called "assignment
>   operators".  The operator= form is called "simple assignment".
>   A special case of the simple assignment is also called "copy
>   assignment".  Correct so far?
>
>   In 13.5.3 the Standard says "An assignment operator shall be
>   implemented by a non-static member function with exactly one
>   parameter".  Then, "Any assignment operator [..] can be virtual"
>
> It seems that there is no distinction between operator= and other
> operator@= in the Standard WRT how they should be overloaded for
> classes.  Still, it seems that this code
>
>    struct A {};
>    A& operator +=(A&, A const&);
>    int main() {
>       A a, b;
>       a += b;
>    }
>
> is not reported as ill-formed.

Yes, it is. It's implicit in the use of the word "shall" in 13.5.3/1. An
implementation of an assignment operator as a non-member function (which
is not a "non-static member function with exactly one parameter")
violates 13.5.3/1 and so makes the program ill-formed.

> Have I missed a defect report somewhere?  Has it been already
> clarified that operators += are not _really_ assignment when it
> comes to overloading and are allowed to be implemented as non-
> member functions?  Or should it have been clear to me because all
> subclauses introducing restrictions to operator= do in fact mention
> operator= explicitly and that means that other assignment operators
> are not limited by those restrictions?

I don't understand your perplexity. The list of assigment operators is
clearly and unambiguosly defined in 5.17. There are additional clauses
clarifying operator= semantic simply because it can be implicitly
generated by the compiler. Of course, clauses explicitly mentioning
operator= do not apply to other kinds of assignment operators. However,
the sentence that answers your doubts (the first one in 13.5.3/1) refers
to "an assignment operator", i.e.: any kind.

Regards,

Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Fri, 30 Jul 2004 18:04:14 GMT
Raw View
Alberto Barbati wrote:
> Victor Bazarov wrote:
>
>> Forgive my ignorance in this matter.  Need clarification on this
>> simple issue:
>>
>>   In 5.17 all operators that have the form @= where '@' is either
>>   empty or one of [ + - << >> * % / & | ^ ] are called "assignment
>>   operators".  The operator= form is called "simple assignment".
>>   A special case of the simple assignment is also called "copy
>>   assignment".  Correct so far?
>>
>>   In 13.5.3 the Standard says "An assignment operator shall be
>>   implemented by a non-static member function with exactly one
>>   parameter".  Then, "Any assignment operator [..] can be virtual"
>>
>> It seems that there is no distinction between operator= and other
>> operator@= in the Standard WRT how they should be overloaded for
>> classes.  Still, it seems that this code
>>
>>    struct A {};
>>    A& operator +=(A&, A const&);
>>    int main() {
>>       A a, b;
>>       a += b;
>>    }
>>
>> is not reported as ill-formed.
>
>
> Yes, it is. It's implicit in the use of the word "shall" in 13.5.3/1. An
> implementation of an assignment operator as a non-member function (which
> is not a "non-static member function with exactly one parameter")
> violates 13.5.3/1 and so makes the program ill-formed.
>
>> Have I missed a defect report somewhere?  Has it been already
>> clarified that operators += are not _really_ assignment when it
>> comes to overloading and are allowed to be implemented as non-
>> member functions?  Or should it have been clear to me because all
>> subclauses introducing restrictions to operator= do in fact mention
>> operator= explicitly and that means that other assignment operators
>> are not limited by those restrictions?
>
>
> I don't understand your perplexity. The list of assigment operators is
> clearly and unambiguosly defined in 5.17. There are additional clauses
> clarifying operator= semantic simply because it can be implicitly
> generated by the compiler. Of course, clauses explicitly mentioning
> operator= do not apply to other kinds of assignment operators. However,
> the sentence that answers your doubts (the first one in 13.5.3/1) refers
> to "an assignment operator", i.e.: any kind.

Thanks, Alberto.  My perplexity is not due to my [mis-]reading of the
Standard, it's in the fact that at least four major compilers (VC++ 7.1),
GCC v3.2, Intel C/C++ v8.0, and Comeau v4.3.3 (online), consider that
code in my original post _well-formed_.  They do not report any errors.

How about that?

Victor

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: johnchx2@yahoo.com (johnchx)
Date: Sat, 31 Jul 2004 00:08:53 GMT
Raw View
v.Abazarov@comAcast.net (Victor Bazarov)

>
> Have I missed a defect report somewhere?

Possibly:

  http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#221

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net ("Victor Bazarov")
Date: Sat, 31 Jul 2004 16:59:18 GMT
Raw View
"johnchx" <johnchx2@yahoo.com> wrote...
> v.Abazarov@comAcast.net (Victor Bazarov)
>
> >
> > Have I missed a defect report somewhere?
>
> Possibly:
>
>   http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#221

Thank you.  That means that I did miss it (and related issues).

V

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]