From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,ab1d177a5a26577d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: What's wrong with C++? Date: Thu, 17 Feb 2011 22:32:48 +0100 Organization: A noiseless patient Spider Message-ID: <87lj1ez6rj.fsf@ludovic-brenta.org> References: <1ee1a434-4048-48f6-9f5e-d8126bebb808@r19g2000prm.googlegroups.com> <4D5C1824.3020509@obry.net> <21443638-5ec6-49d4-aafe-6fbc1e59daba@r19g2000prm.googlegroups.com> <87d2371e-af91-4d6a-8d5b-3ddb972d84fd@k17g2000pre.googlegroups.com> <87zkpuze5e.fsf@ludovic-brenta.org> <4d5d791f$0$17330$882e7ee2@usenet-news.net> <87vd0izba1.fsf@ludovic-brenta.org> <4d5d8112$0$17330$882e7ee2@usenet-news.net> <87pqqqz8o6.fsf@ludovic-brenta.org> <4d5d8d41$0$17330$882e7ee2@usenet-news.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="h3lW1vp1zj3M9XETvzOiKw"; logging-data="7840"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hfbkpfQwQUWPARCwM9JPs" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Cancel-Lock: sha1:d5OvKeHOaJjpZsVOej2u15mpwz8= sha1:Tvzqbo1jAiaYAIVHTX/wjxvbfOw= Xref: g2news2.google.com comp.lang.ada:18347 Date: 2011-02-17T22:32:48+01:00 List-Id: Hyman Rosen writes on comp.lang.ada: > On 2/17/2011 3:51 PM, Ludovic Brenta wrote: >> Hyman Rosen writes: >>> C and C++ enumerations don't have arithmetic. >>> They do convert automatically to integers, but >>> they don't automatically convert back. >> >> Actually they do in the example you provided: >> >> struct DamageType { >> enum E { Fire = 1, Acid = 2, Lightning = 4, Poison = 8 }; >> }; >> >> DamageType::E what_my_dragon_can_do = >> DamageType::E( DamageType::Acid | DamageType::Poison ); >> >> In this example, the two enumeration values are automatically promoted >> to integer (which has arithmetic) ORed, then the result is automatically >> converted back to the type DamageType::E. > > How is that automatic? It is explicitly converted back through > DamageType::E( expression ) Because, unless my C++ is really rusty, this is not a conversion. It is an explicit call to the (implicitly defined) copy constructor DamageType::E (DamageType::E&) where the parameter, of type int, is automatically converted to DamageType::E&. An explicit type conversion would look like: (DamageType::E) expression >> Except that the value of what_my_dragon_can_do is not one of the >> values defined in the type. > > Once again, this is a design decision made by those who brought > enumerations into C. The value an enumeration may take is not > restricted to being one of the enumerators because the language wants > to support using enumeration literals as bitmasks. You may not like > it, but this use is correct C++. Agreed. That was my point: C++ is a dangerous language, by design. -- Ludovic Brenta.