Topic: bool to enum conversion


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/09
Raw View
In article <7tlcua$2lo@enews4.newsguy.com>, Darin Adler
<darin@bentspoon.com> writes
>Francis Glassborow <francis@robinton.demon.co.uk> wrote:
>> I think my newsreader must be censoring my news because I cannot see any
>> need for conversions/promotions or whatever from a bool.  foo(0) is a
>> call to foo with an int.
>
>The overloaded function is f, not foo.

As several have pointed out to me in email.  It made me consider why I
had misread the code.  (and I gather that I was not the only one who did
so at first reading, I just didn't read it carefully the second and
third time:).  I think that the code would have been less open to
misreading had f( been written as f (.  I think that extra whitespace is
very helpful in this situation.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/08
Raw View
In article <7thb95$jer@enews3.newsguy.com>, Darin Adler
<darin@bentspoon.com> writes
>> Here is the test code:
>>
>> #include <stdio.h>
>> enum enum_type { a=0, b=1};
>> void f(enum_type) {printf("ENUM");}
>> void f(char) {printf("CHAR");}
>> void foo(int x){ f(x==1);}
>> int main(){ foo(0);}
>>
>> gcc produces CHAR while BCB gives error about ambiguous call.
>
>gcc is correct. BCB (whatever that is) is incorrect. I tried the example on
>my favorite compiler and it gets it right too.
>
>If you add a third overload of f
>
>    void f(int) {printf("INT")};
>
>you should get INT, because bool -> int is a promotion rather than a
>conversion.

I think my newsreader must be censoring my news because I cannot see any
need for conversions/promotions or whatever from a bool.  foo(0) is a
call to foo with an int.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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              ]





Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/10/08
Raw View
>>> Here is the test code:
>>>
>>> #include <stdio.h>
>>> enum enum_type { a=0, b=1};
>>> void f(enum_type) {printf("ENUM");}
>>> void f(char) {printf("CHAR");}
>>> void foo(int x){ f(x==1);}
>>> int main(){ foo(0);}
>>>
>>> gcc produces CHAR while BCB gives error about ambiguous call.

Darin Adler <darin@bentspoon.com> wrote:
>>gcc is correct. BCB (whatever that is) is incorrect. I tried the example on
>>my favorite compiler and it gets it right too.
>>
>>If you add a third overload of f
>>
>>    void f(int) {printf("INT")};
>>
>>you should get INT, because bool -> int is a promotion rather than a
>>conversion.

Francis Glassborow <francis@robinton.demon.co.uk> wrote:
> I think my newsreader must be censoring my news because I cannot see any
> need for conversions/promotions or whatever from a bool.  foo(0) is a
> call to foo with an int.

The overloaded function is f, not foo.

    -- Darin


[ 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              ]






Author: gbush@my-deja.com
Date: 1999/10/07
Raw View
Is bool to enum conversion allowed by standard? I see 4.7p4 where it's
said that bool is converted to 0 or 1. Is it speaking only about bool to
int conversion? Then it's already mentioned in 4.5p4 as promotion. I'm
confused.
Here is the test code:

#include <stdio.h>
enum enum_type { a=0, b=1};
void f(enum_type) {printf("ENUM");}
void f(char) {printf("CHAR");}
void foo(int x){ f(x==1);}
int main(){ foo(0);}

gcc produces CHAR while BCB gives error about ambiguous call.
Who is correct?

Gene Bushuyev.


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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              ]





Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/10/07
Raw View
In article <7tgr3n$no5$1@nnrp1.deja.com>, gbush@my-deja.com wrote:

> Is bool to enum conversion allowed by standard?

No. There's no bool to enum conversion.

> I see 4.7p4 where it's said that bool is converted to 0 or 1. Is it speaking
> only about bool to int conversion? Then it's already mentioned in 4.5p4 as
> promotion. I'm confused.

4.5p4 talks about promotion from bool to int.

The second half of 4.7p4 talks about conversion from bool to any integer
type. 4.7p1 explains what an integral conversion is and that's what makes it
clear that it's a conversion to any integral type. Integer types include
unsigned int, short int, and others (listed in 3.9.1p7). 4.7p5 specifically
excludes any integral  conversion that's already available as an integral
promotion. Thus bool to int is an integral promotion but not an integral
conversion.

> Here is the test code:
>
> #include <stdio.h>
> enum enum_type { a=0, b=1};
> void f(enum_type) {printf("ENUM");}
> void f(char) {printf("CHAR");}
> void foo(int x){ f(x==1);}
> int main(){ foo(0);}
>
> gcc produces CHAR while BCB gives error about ambiguous call.

gcc is correct. BCB (whatever that is) is incorrect. I tried the example on
my favorite compiler and it gets it right too.

If you add a third overload of f

    void f(int) {printf("INT")};

you should get INT, because bool -> int is a promotion rather than a
conversion.

    -- Darin


[ 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              ]






Author: wmm@fastdial.net
Date: 1999/10/07
Raw View
In article <7tgr3n$no5$1@nnrp1.deja.com>,
  gbush@my-deja.com wrote:
> Is bool to enum conversion allowed by standard? I see 4.7p4 where it's
> said that bool is converted to 0 or 1. Is it speaking only about bool
to
> int conversion? Then it's already mentioned in 4.5p4 as promotion. I'm
> confused.

4.5p4 is talking only about when the target type is "int"; 4.7p4 is
dealing with conversions of "bool" to other integral types (see
4.7p5).  (The distinction between promotion and conversion makes
a difference in overload resolution; see the table in 13.3.3.1.1p3.
A bool ==> int promotion is a better match than a bool ==> short
conversion, even though the value of the conversion is the same in
both cases.)

> Here is the test code:
>
> #include <stdio.h>
> enum enum_type { a=0, b=1};
> void f(enum_type) {printf("ENUM");}
> void f(char) {printf("CHAR");}
> void foo(int x){ f(x==1);}
> int main(){ foo(0);}
>
> gcc produces CHAR while BCB gives error about ambiguous call.
> Who is correct?

gcc is correct; there are no implicit conversions to enum type,
only explicit conversions (7.2p9).  Thus f(enum_type) is not a
viable function with a bool argument (13.3.2p3).
--
William M. Miller, wmm@fastdial.net


Sent via Deja.com http://www.deja.com/
Before you buy.


[ 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              ]