Topic: operator overloading and enumeration t


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/03/01
Raw View
In article fsf@concmp.com, Luddy Harrison <luddy@concmp.com> writes:
>I'm afraid the typos in my original post have obscured the question I
>intended to ask; let me try again.
>
>Is this legal?
>
>     enum color { red, blue };
>     void f() { color x = red; x += 1; }
>
>How about this?
>
>     enum color { red, blue };
>     void f() { color x = red; x++; }

Neither version of f is valid unless you define appropriate overloaded
operators += and ++.

>Section 13-24, paragraph 19 of CD2 seems to permit the former.

There is no section 13-24. I'll assume you mean page 13-24. You
are correct that paragraph 19 on that page in CD-2 says "arithmetic
or enum types", but that sentence was corrected in the FDIS to say
just "arithmetic types". (By definition in 3.9 "Types", enums are
not arithmetic types.)

At the beginning of the section you are referring to (13.6 "Built   in
operators") it says that these descriptions are only for the purpose
of determining operator overloading, and not for any other purpose.
It is possible for overloading to resolve to a function that
cannot be called. Chapter 5 "Expressions", defines the requirements
on and behavior of the predefined operators. You will find that ++
and += are not predefined for enum types.

---
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]