Compiler Quirk or Language flaw?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Compiler Quirk or Language flaw?

    Hello I was compiling a program, and noticed something that seems
    incredibly odd to me, and am wondering if this is a "Feature" from the
    compiler or possibly there are in fact 2 different datatypes with the
    same name, under the ANSI standard.

    Here is an example.

    bool MyFunction(int MyVar);

    BOOL MyFunction(int MyVar){
    if(MyVar <=5){
    return(TRUE);
    }else{
    return(FALSE);
    }

    The problem I am having is that BOOL and bool appear to my compiler to
    be different datatypes, I am using VS.NET 2003.

    If I change case on either one it works fine as long as the case
    matches.
    I realize C/C++ are case sensitive, but I would expect one or the
    other to simply cause the compiler to kick out with "Undefined
    Datatype "BOOL" on line 5" or something along those lines, instead it
    complains that I am overloading and the only difference is in the
    return type.

    Can someone please explain what is going on here?
  • Howard

    #2
    Re: Compiler Quirk or Language flaw?


    "Steve" <gr82meetu78@ya hoo.com> wrote in message
    news:bb8f15bd.0 404151133.5cd10 ab0@posting.goo gle.com...[color=blue]
    > Hello I was compiling a program, and noticed something that seems
    > incredibly odd to me, and am wondering if this is a "Feature" from the
    > compiler or possibly there are in fact 2 different datatypes with the
    > same name, under the ANSI standard.
    >
    > Here is an example.
    >
    > bool MyFunction(int MyVar);
    >
    > BOOL MyFunction(int MyVar){
    > if(MyVar <=5){
    > return(TRUE);
    > }else{
    > return(FALSE);
    > }
    >
    > The problem I am having is that BOOL and bool appear to my compiler to
    > be different datatypes, I am using VS.NET 2003.
    >
    > If I change case on either one it works fine as long as the case
    > matches.
    > I realize C/C++ are case sensitive, but I would expect one or the
    > other to simply cause the compiler to kick out with "Undefined
    > Datatype "BOOL" on line 5" or something along those lines, instead it
    > complains that I am overloading and the only difference is in the
    > return type.
    >
    > Can someone please explain what is going on here?[/color]

    The bool type is part of the standard. The BOOL datatype is "user-defined",
    or in your case, more specifically "implementa tion defined", in that some
    part of your compiler's libraries probably defines the type BOOL. It may
    defined it as bool or as unsigned int (or anything else they wanted), but in
    any case it's a different type, and so you're getting expected behavior.
    Same as if you used unsigned int versus bool.

    -Howard



    Comment

    • red floyd

      #3
      Re: Compiler Quirk or Language flaw?

      Howard wrote:[color=blue]
      > "Steve" <gr82meetu78@ya hoo.com> wrote in message
      > news:bb8f15bd.0 404151133.5cd10 ab0@posting.goo gle.com...
      >[color=green]
      >>Hello I was compiling a program, and noticed something that seems
      >>incredibly odd to me, and am wondering if this is a "Feature" from the
      >>compiler or possibly there are in fact 2 different datatypes with the
      >>same name, under the ANSI standard.
      >>
      >>Here is an example.
      >>
      >>bool MyFunction(int MyVar);
      >>
      >>BOOL MyFunction(int MyVar){
      >> if(MyVar <=5){
      >> return(TRUE);
      >> }else{
      >> return(FALSE);
      >>}
      >>
      >>The problem I am having is that BOOL and bool appear to my compiler to
      >>be different datatypes, I am using VS.NET 2003.
      >>
      >>If I change case on either one it works fine as long as the case
      >>matches.
      >>I realize C/C++ are case sensitive, but I would expect one or the
      >>other to simply cause the compiler to kick out with "Undefined
      >>Datatype "BOOL" on line 5" or something along those lines, instead it
      >>complains that I am overloading and the only difference is in the
      >>return type.
      >>
      >>Can someone please explain what is going on here?[/color]
      >
      >
      > The bool type is part of the standard. The BOOL datatype is "user-defined",
      > or in your case, more specifically "implementa tion defined", in that some
      > part of your compiler's libraries probably defines the type BOOL. It may
      > defined it as bool or as unsigned int (or anything else they wanted), but in
      > any case it's a different type, and so you're getting expected behavior.
      > Same as if you used unsigned int versus bool.
      >
      > -Howard
      >
      >
      >[/color]
      Microsoft defines BOOL as

      typedef int BOOL;

      bool is defined by the Holy Standard. BOOL is defined by Microsoft.

      Comment

      • Tim Slattery

        #4
        Re: Compiler Quirk or Language flaw?

        gr82meetu78@yah oo.com (Steve) wrote:

        [color=blue]
        >The problem I am having is that BOOL and bool appear to my compiler to
        >be different datatypes, I am using VS.NET 2003.[/color]

        They are different datatypes. "bool" is a primitive data type defined
        by the C++ standard, as are the constants "true" and "false". "BOOL"
        is defined someplace in the complex of files that windows.h pulls in
        to be an int. The values "TRUE" and "FALSE" are defined the same place
        to be used with it. BOOL goes back to the early days of the Windows
        SDK, before C++ and the "bool" datatype existed. There was a need for
        a boolean datatype, and this is how MS dealt with it. BOOL is still
        heavily used by the Windows API functions.

        --
        Tim Slattery
        Slattery_T@bls. gov

        Comment

        • John Harrison

          #5
          Re: Compiler Quirk or Language flaw?


          "Steve" <gr82meetu78@ya hoo.com> wrote in message
          news:bb8f15bd.0 404151133.5cd10 ab0@posting.goo gle.com...[color=blue]
          > Hello I was compiling a program, and noticed something that seems
          > incredibly odd to me, and am wondering if this is a "Feature" from the
          > compiler or possibly there are in fact 2 different datatypes with the
          > same name, under the ANSI standard.
          >
          > Here is an example.
          >
          > bool MyFunction(int MyVar);
          >
          > BOOL MyFunction(int MyVar){
          > if(MyVar <=5){
          > return(TRUE);
          > }else{
          > return(FALSE);
          > }
          >
          > The problem I am having is that BOOL and bool appear to my compiler to
          > be different datatypes, I am using VS.NET 2003.
          >[/color]

          What gave you the idea that BOOL is part of any standard? BOOL is
          non-standard as are TRUE and FALSE.
          [color=blue]
          > If I change case on either one it works fine as long as the case
          > matches.
          > I realize C/C++ are case sensitive, but I would expect one or the
          > other to simply cause the compiler to kick out with "Undefined
          > Datatype "BOOL" on line 5" or something along those lines, instead it
          > complains that I am overloading and the only difference is in the
          > return type.
          >
          > Can someone please explain what is going on here?[/color]

          Microsoft typedef BOOL as int, which is obviously different from bool. Don't
          use non-standard code when you don't have to.

          john


          Comment

          • Andrey Tarasevich

            #6
            Re: Compiler Quirk or Language flaw?

            Steve wrote:[color=blue]
            > Hello I was compiling a program, and noticed something that seems
            > incredibly odd to me, and am wondering if this is a "Feature" from the
            > compiler or possibly there are in fact 2 different datatypes with the
            > same name, under the ANSI standard.
            >
            > Here is an example.
            >
            > bool MyFunction(int MyVar);
            >
            > BOOL MyFunction(int MyVar){
            > if(MyVar <=5){
            > return(TRUE);
            > }else{
            > return(FALSE);
            > }
            >
            > The problem I am having is that BOOL and bool appear to my compiler to
            > be different datatypes, I am using VS.NET 2003.
            >
            > If I change case on either one it works fine as long as the case
            > matches.
            > I realize C/C++ are case sensitive, but I would expect one or the
            > other to simply cause the compiler to kick out with "Undefined
            > Datatype "BOOL" on line 5" or something along those lines, instead it
            > complains that I am overloading and the only difference is in the
            > return type.
            > ...[/color]

            There's no such thing as 'BOOL' in C++. If your compiler recognizes this
            type in your code, it must be user-defined in some vendor-specific
            library header file. It won't be surprising if it is defined to be
            something else than 'bool'.

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • Ioannis Vranos

              #7
              Re: Compiler Quirk or Language flaw?

              "Steve" <gr82meetu78@ya hoo.com> wrote in message
              news:bb8f15bd.0 404151133.5cd10 ab0@posting.goo gle.com...[color=blue]
              > Hello I was compiling a program, and noticed something that seems
              > incredibly odd to me, and am wondering if this is a "Feature" from the
              > compiler or possibly there are in fact 2 different datatypes with the
              > same name, under the ANSI standard.
              >
              > Here is an example.
              >
              > bool MyFunction(int MyVar);
              >
              > BOOL MyFunction(int MyVar){
              > if(MyVar <=5){
              > return(TRUE);
              > }else{
              > return(FALSE);
              > }
              >
              > The problem I am having is that BOOL and bool appear to my compiler to
              > be different datatypes, I am using VS.NET 2003.[/color]


              Doesn't VS include a help file?


              Platform SDK: Windows API

              ....


              ATOM Atom. For more information, see Atoms.
              BOOL Boolean variable (should be TRUE or FALSE).
              BOOLEAN Boolean variable (should be TRUE or FALSE).




              It is Win32 type, not .NET. Also as it is known C++ distinguishes between
              uppercase and lowercase, so BOOL, BoOl, Bool, bool etc are different
              identifiers. The ISO C++ type is bool which is the case of .NET is also the
              ..NET boolean type. (.NET types map directly to the default types of the
              various supported languages, where the are equivalent default types of
              course).






              Ioannis Vranos

              Comment

              • Steve

                #8
                Re: Compiler Quirk or Language flaw?

                > What gave you the idea that BOOL is part of any standard? BOOL is[color=blue]
                > non-standard as are TRUE and FALSE.
                > Microsoft typedef BOOL as int, which is obviously different from bool. Don't
                > use non-standard code when you don't have to.
                >
                > john[/color]

                So how SHOULD this have been written, so that it's fully compatible
                with the current standard?

                Comment

                • John Harrison

                  #9
                  Re: Compiler Quirk or Language flaw?


                  "Steve" <gr82meetu78@ya hoo.com> wrote in message
                  news:bb8f15bd.0 404152021.4cb4b c40@posting.goo gle.com...[color=blue][color=green]
                  > > What gave you the idea that BOOL is part of any standard? BOOL is
                  > > non-standard as are TRUE and FALSE.
                  > > Microsoft typedef BOOL as int, which is obviously different from bool.[/color][/color]
                  Don't[color=blue][color=green]
                  > > use non-standard code when you don't have to.
                  > >
                  > > john[/color]
                  >
                  > So how SHOULD this have been written, so that it's fully compatible
                  > with the current standard?[/color]

                  bool MyFunction(int MyVar);

                  bool MyFunction(int MyVar){
                  if(MyVar <=5){
                  return(true);
                  }else{
                  return(false);
                  }

                  or even more simply

                  bool MyFunction(int MyVar){
                  return MyVar <= 5;
                  }

                  john


                  Comment

                  Working...