Implicit conversion of bool to int
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Implicit conversion of bool to int

  1. #1
    Join Date
    May 2002
    Posts
    1,435

    Implicit conversion of bool to int

    I have a class A with member function X():
    Code:
    class A
    {
    	public:
    		void X(int x, bool b=true)
    		{
    		}
    };
    My code calls A::X() sometimes with an explicit bool parameter:
    Code:
    class A;
    a.X(1,true);
    a.X(1,false);
    Now I need to add a second integer parameter to A:X():
    Code:
    class A
    {
    	public:
    		void X(int x, int y, bool b=true)
    		{
    		}
    };
    The problem is that when I re-compile I am expecting the calls to A::X() above to be flagged as implicitly converting a bool to an int:
    Code:
    class A;
    a.X(1,true);   // WARNING: converting bool to int
    a.X(1,false);   // WARNING: converting bool to int
    But it doesn't happen - the code compiles without errors or warnings. I have my warning level set to 4 (the highest). This is a huge project that I have been maintaining for over ten years and much of the code dates back at least ten years before that so I cannot make changes to the build configurations - I just want to zero-in on this particular problem at the compile stage. How can I do it?

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Implicit conversion of bool to int

    That's weird and I would think that the compiler would have caught that, but who knows. When I've had to do such things, I usually try a search/replace project wide with a clever regex to do the dirty work, but I have had instances where I needed to do something similar to what you describe. In those cases, I declared it as a completely different type, like char * for instance, then the compiler definitely catches it. After the compiler finds all the nonconvertible types, I change it to the type I want. It's double the work it should be, but it definitely catches them all.

  3. #3
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Implicit conversion of bool to int

    That is one of the problems with providing default parameters. The few seconds it saves in typing (when invoking the method) can result in major heartache and pain later. I stopped using them about a decade ago (and took the time to remove them from each project as I worked on them).

    Does you new routine HAVE to be an overload of the Existing One????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Implicit conversion of bool to int

    Also, now that I think of it, hasn't the bool type changed between VC6.0 and VS2003 (or maybe between VS2003 and VS2005). I remember running into some hassles with old code and the bool type. We've since replaced all bool, BOOL, and boolean with int in all our projects to avoid this. The one problem is with OLE where a VariantBool is -1=true and 0=false. *** microsoft?

  5. #5
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Implicit conversion of bool to int

    Quote Originally Posted by hoxsiew
    Also, now that I think of it, hasn't the bool type changed between VC6.0 and VS2003 (or maybe between VS2003 and VS2005). I remember running into some hassles with old code and the bool type. We've since replaced all bool, BOOL, and boolean with int in all our projects to avoid this. The one problem is with OLE where a VariantBool is -1=true and 0=false. *** microsoft?
    BOOL, bool, and VariantBool are three different datatypes. THere should be NO expectation of transparent mix and match.

    Also replacing bool with int, can cause diferences in behaviour (BOOL is int, and VariantBool is implemented as int with conventions).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center