Topic: Understanding the behavior of new Bool Type


Author: kcline@sun132.spd.dsccc.com (Kevin Cline)
Date: 1996/06/28
Raw View
In article <4qv28r$c78@engnews1.Eng.Sun.COM>,
Chris P. Randle <randle@cig.mot.com> wrote:
>It is my understanding that an enum variable cannot be assigned an
>interger value:
>        myEnum = myInt; //ERROR
>1) Does this mean I should cast line #12 to this:
>        return bool(5 < 3)?

bool is not an enumeration.  It is a new integer type, like char and short.

>2) This question is really related to question 1. If I wrap a
>C function that is to suppose return 1 on failure and 0 success, into a
>C++ member that returns a bool, do I again cast the return value:
>
>
>bool MyObject::memberFuncCallingCfunction {
>
>        return bool(C_function());

 return C_function(); // simplest, and probably best

>        // or do I have to do this:
>       // int retval = C_function();
>       // if (retval == 0)
>       //    return true;
>       // else
>       //    return false;
>}



>
>3) With GNU 2.7.2 the following sequence compiles and works.
>Is the behavior below part of the standard?

Yes.

>More explicitly,
>setting a bool to a nonzero interger is the same as setting
>a bool to the manifest constant "true"

Correct.

>lines 4-7 shown again for clarity:
> 4  bool b02;
> 5  b02 = 3;
> 6  if (b02 == true)

It is poor coding style to test booleans for equality
with true and false.  true and false should only be
used to set the value of a boolean variable.

Just write
 if (b02)

--
Kevin Cline
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Stan Sulsky <sjs@curtech.com>
Date: 1996/07/01
Raw View
kcline@sun132.spd.dsccc.com (Kevin Cline) wrote:
>
>It is poor coding style to test booleans for equality
>with true and false.  true and false should only be
>used to set the value of a boolean variable.

No doubt this is sound advice.  As a language design decision,
though, doesnt' this strike anyone but me as a little weird?

 [Moderator's note: it's not a language design decision.
  Draft standard C++ allows you to test booleans for equality
  with true and false, and guarantees that the results will be
  as one would expect: if `x' is an expression of type bool,
  then `x == true' means the same as `x' and `x == false' is the
  same as `!x'.  The rule mentioned by Kevin Cline is probably a
  good one, but discussion of style belongs on
  comp.lang.c++.moderated rather than here.  -fjh.]

--
Stan Sulsky
          Current Technology, Inc.  |   -- sjs@curtech.com --
          97 Madbury Rd.            |   (603) 868-2270 - voice
          Durham, NH 03824   USA    |   (603) 868-1352 - fax
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Chris P. Randle" <randle@cig.mot.com>
Date: 1996/06/27
Raw View
It is my understanding that an enum variable cannot be assigned an
interger value:
        myEnum = myInt; //ERROR
1) Does this mean I should cast line #12 to this:
        return bool(5 < 3)?

2) This question is really related to question 1. If I wrap a
C function that is to suppose return 1 on failure and 0 success, into a
C++ member that returns a bool, do I again cast the return value:


bool MyObject::memberFuncCallingCfunction {

        return bool(C_function());
        // or do I have to do this:
       // int retval = C_function();
       // if (retval == 0)
       //    return true;
       // else
       //    return false;
}

3) With GNU 2.7.2 the following sequence compiles and works.
Is the behavior below part of the standard? More explicitly,
setting a bool to a nonzero interger is the same as setting
a bool to the manifest constant "true":


lines 4-7 shown again for clarity:
 4  bool b02;
 5  b02 = 3;
 6  if (b02 == true)
 7    cout << "b02 == true (b02=3)\n"; //this gets printed

Thanks for your help!


===sample:

1 bool foo(void) {
2  bool b01;
3  b01 = 1;
4  bool b02;
5  b02 = 3;
6  if (b02 == true)
7    cout << "b02 == true (b02=3)\n"; //this gets printed
8  bool b03;
9  b03 = 0;
10  if (b03 == false)
11  cout << "b03 == false (b03=0)\n"; //this gets printed
12  return (5 < 3);
13 }


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/06/28
Raw View
In article <4qv28r$c78@engnews1.Eng.Sun.COM> "Chris P. Randle"
<randle@cig.mot.com> writes:

|> It is my understanding that an enum variable cannot be assigned an
|> interger value:
|>         myEnum = myInt; //ERROR

The new bool type is not an enum, but a totally new type.  While I
don't think it can be assigned to an enum, it converts to int
implicitly.

|> 1) Does this mean I should cast line #12 to this:
|>         return bool(5 < 3)?

The results of a comparison operator *ARE* of type bool.  (This was
changed with the introduction of the type.)

|> 2) This question is really related to question 1. If I wrap a
|> C function that is to suppose return 1 on failure and 0 success, into a
|> C++ member that returns a bool, do I again cast the return value:


|> bool MyObject::memberFuncCallingCfunction {

|>         return bool(C_function());
|>         // or do I have to do this:
|>        // int retval = C_function();
|>        // if (retval == 0)
|>        //    return true;
|>        // else
|>        //    return false;
|> }

Well, you have to do something.  The implicit conversion int->bool is
that 0->false, and anything else to true.  Which is just the opposite
of what happens in your function.  You could juse write:

 return ! C_function() ;

|> 3) With GNU 2.7.2 the following sequence compiles and works.
|> Is the behavior below part of the standard? More explicitly,
|> setting a bool to a nonzero interger is the same as setting
|> a bool to the manifest constant "true":


|> lines 4-7 shown again for clarity:
|>  4  bool b02;
|>  5  b02 = 3;
|>  6  if (b02 == true)
|>  7    cout << "b02 == true (b02=3)\n"; //this gets printed

This is part of the implicit conversion of int to bool.  On the other
hand, consider the following:

 bool b = 3 ;
 cout << (int)b << endl ;

It should output 1, not 3.
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils,    tudes et r   alisations en logiciel orient    objet --
                -- A la recherche d'une activit    dans une region francophone



[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]