|
-
September 15th, 2010, 05:08 AM
#16
Re: WHich one is evaluated first
-
September 15th, 2010, 05:33 AM
#17
Re: WHich one is evaluated first
 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>
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.
-
September 15th, 2010, 10:25 AM
#18
Re: WHich one is evaluated first
From the Wikipedia page of C++0x:
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.
Old Unix programmers never die, they just mv to /dev/null
-
September 15th, 2010, 10:58 AM
#19
Re: WHich one is evaluated first
 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 )
?
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.
-
September 15th, 2010, 11:14 AM
#20
Re: WHich one is evaluated first
 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
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
September 15th, 2010, 11:17 AM
#21
Re: WHich one is evaluated first
 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.
 Originally Posted by monarch_dodra
But what about things like
Code:
while( (std::cin >> a) && a!=0 )
Consider:
 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.
 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).
-
September 15th, 2010, 01:16 PM
#22
Re: WHich one is evaluated first
 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.
 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.
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.
-
September 15th, 2010, 01:27 PM
#23
Re: WHich one is evaluated first
 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;
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
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
|