CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    Question Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    Hey guys, I have derived my class from the C++ safe bool idiom class from this page : The Safe Bool Idiom by Bjorn Karlsson

    Code:
    class Element : public safe_bool<>
    {
    public:
    	bool Exists() const;
    	bool boolean_test() const { return Exists(); }; // boolean_test() is a safe_bool method
    }
    When I tried to use it in the if expression like below

    Code:
    Element ele;
    ...
    if(ele)
    I got an error like error C2451: conditional expression of type 'Element' is illegal. If I try to cast it to bool, I got this error,

    Code:
    Element ele;
    ...
    if((bool)ele)

    error C2440: 'type cast' : cannot convert from 'Element' to 'bool'

    This is the 1st time I am using safe bool idiom, I am not sure if this is not allowed or a bug in Visual C++ 10. Any comments?
    Last edited by CBasicNet; December 14th, 2010 at 07:55 AM. Reason: Added missing semicolon to Element ele

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    need to see your safe_bool<> code

    also, you have semicolon bool boolean_test() const { return Exists(); }; <-- here. Why?

    and you missed semicolon from the end of your class dec.

    Your code examples wont compile due to syntax error. suggest you copy your code here, not re-writre it.
    Last edited by Amleto; December 14th, 2010 at 07:32 AM.

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    The sample code at end of the article compiled well in VS2008 after I made the member function this_type_does_not_support_comparisons a public function.

    Did you also copy the template functions?

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    Quote Originally Posted by itsmeandnobodyelse View Post
    The sample code at end of the article compiled well in VS2008 after I made the member function this_type_does_not_support_comparisons a public function.

    Did you also copy the template functions?
    the article says the whole point of that function is that it isnt public.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    If you've been following the evolution of C++, then you should now that safe bool is almost obsolete:

    C++0x provides explicit operators. This means you can cast your object to a bool, but ONLY if you explicitly ask for it.

    This is combined by the fact that in C++0x, expressions that expect a boolean are allowed to make implicit calls to the explicit operator bool.

    The end result, is that your object will never be implicitly cast to bool, but writing "if(my_object)" will still be valid.

    ...

    In my professional experience, I would advise against using a cast to bool though, unless your object models a pointer concept. Providing a member is_valid() is just as easy, and more explicit/clear to users who are not used to using your class.

    As mentioned by the article, the notion of "cast to bool" can be ambiguous, and may be dangerous.

    Its a case by case issue of course, so you'll have to be the judge.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    Quote Originally Posted by Amleto View Post
    the article says the whole point of that function is that it isnt public.
    VS2008 won't return a function pointer of a protected member function.

    Code:
      void f()
      {
         int i = 0;
         Testable_with_virtual twf;
         if (twf)   // compile error
             i++;
         Testable_without_virtual twof;
         if (twof)  // compile error 
             i--;
    
      }
    
    error C2248: 'safe_bool_base::this_type_does_not_support_comparisons' : cannot access protected member declared in class 'safe_bool_base'

  7. #7
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)

    Quote Originally Posted by monarch_dodra View Post
    In my professional experience, I would advise against using a cast to bool though, unless your object models a pointer concept. Providing a member is_valid() is just as easy, and more explicit/clear to users who are not used to using your class.

    As mentioned by the article, the notion of "cast to bool" can be ambiguous, and may be dangerous.

    Its a case by case issue of course, so you'll have to be the judge.
    I guess you are right! I have dropped the idea of using the safe bool idiom. My library users can call Exists() method.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured