Re: WHich one is evaluated first
Quote:
Originally Posted by
laserlight
No. If you insist otherwise, then be prepared: a standard library implementation that conforms to what is expected to be the next version of the C++ standard will cause your code snippet to fail to compile ;)
Just to be more specific ( and to see if I got it right :) ), in the next standard implicit boolean conversions are treated as direct initializations of the boolean target ( that is "bool b(t);" and not "bool b = t;" ). For this reason that code * would compile with an explicit bool conversion function (laserlight, correct me if I'm wrong :) ).
*: I mean the "if( std::cin )" snippet
Re: WHich one is evaluated first
Quote:
Originally Posted by
laserlight
No. If you insist otherwise, then be prepared: a standard library implementation that conforms to what is expected to be the next version of the C++ standard will cause your code snippet to fail to compile ;)
Holy crap, you're right!
Code:
27.5.4 Class template basic_ios [ios]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ios : public ios_base {
public:
// types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
explicit operator bool() const;
bool operator!() const;
iostate rdstate() const;
void clear(iostate state = goodbit);
void setstate(iostate state);
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;
That's gonna break a lot of code!
Guess I'll just use
Much faster than typing static_cast<bool> ;)
Re: WHich one is evaluated first
From the Wikipedia page of C++0x:
Quote:
In C++0x, the explicit keyword can now be applied to conversion operators. As with constructors, it prevents the use of those conversion functions in implicit conversions. However, language contexts that specifically require a boolean value (the conditions of if-statements and loops, as well as operands to the logical operators) count as explicit conversions and can thus use a bool conversion operator.
So it's not that bad.
And as far as I'm aware, the C++0x committee goes out of its way not to break existing code.
Re: WHich one is evaluated first
Quote:
Originally Posted by
HighCommander4
From the Wikipedia page of C++0x:
So it's not that bad.
And as far as I'm aware, the C++0x committee goes out of its way not to break existing code.
I see. But what about things like
Code:
while( (std::cin >> a) && a!=0 )
?
Re: WHich one is evaluated first
Quote:
Originally Posted by monarch_dodra
Much faster than typing static_cast<bool>
but what about (bool) ? and !! isn't that informative on what that actually means :)
Re: WHich one is evaluated first
Quote:
Originally Posted by monarch_dodra
I see.
Err... I was being serious. As far as I know, explicit conversion functions to bool are specifically designed to replace the safe bool idiom and other existing methods to try and convert to bool without allowing unexpected behaviour, i.e., it is the case that it is not going to break existing code.
Quote:
Originally Posted by monarch_dodra
But what about things like
Code:
while( (std::cin >> a) && a!=0 )
Consider:
Quote:
Originally Posted by C++ Standard Working Draft 2010-08-21 Clause 4 Paragraph 3 (part)
An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t. Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t. The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.
Quote:
Originally Posted by C++ Standard Working Draft 2010-08-21 Clause 5.14 Paragraph 1 (part)
The && operator groups left-to-right. The operands are both contextually converted to type bool (Clause 4).
Re: WHich one is evaluated first
Quote:
Originally Posted by
Joeman
but what about (bool) ? and !! isn't that informative on what that actually means :)
well, bool is a C-cast. I stay clear away from those. !! is just as informative as static_cast<bool> if you're used to them. As always, it is just a language construct you have seen before and understand, or it isn't and you have to think about it. Were I work, we have objects that can't be cast to bool, but have operator!. We use "if (!!a)" all the time, and when we see "!!" we know exactly what it means. And it's safe.
Quote:
Originally Posted by
laserlight
Err... I was being serious. As far as I know, explicit conversion functions to bool are specifically designed to replace the safe bool idiom and other existing methods to try and convert to bool without allowing unexpected behaviour, i.e., it is the case that it is not going to break existing code.
I was acknowledging what was being said to me. Maybe "I see" can be interpreted as sarcasm in some context, but this was not the case here.
Thank you very much. You have taught me two very interesting things today. Pardon me if my surprised looked like mockery. It wasn't.
Re: WHich one is evaluated first
Quote:
Originally Posted by monarch_dodra
well, bool is a C-cast. I stay clear away from those
Yes, static_cast is preferable, but when it comes to the primitive types, the c-cast works fine. I only worry when it comes to polymorphism and class pointers in general.
I would hate to write this all over my code
Code:
int I = 10;
int I2 = 3;
float F = static_cast<float>( I ) / I2;
vs
Code:
int I = 10;
int I2 = 3;
float F = (float) I / I2;