-
December 14th, 2010, 04:55 AM
#1
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
-
December 14th, 2010, 07:30 AM
#2
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.
-
December 14th, 2010, 07:45 AM
#3
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?
-
December 14th, 2010, 07:46 AM
#4
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.
-
December 14th, 2010, 08:04 AM
#5
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.
-
December 14th, 2010, 08:10 AM
#6
Re: Safe Boolean Idiom cannot compile with Visual C++ 10(2010)
Originally Posted by Amleto
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'
-
December 14th, 2010, 07:28 PM
#7
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|