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 06:55 AM.
Reason: Added missing semicolon to Element ele
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.
Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)
Originally Posted by itsmeandnobodyelse
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.
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 LITE 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.
Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)
Originally Posted by monarch_dodra
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.
Bookmarks