Is C really faster than c++??

Pages: 12
There is no reason that

1
2
template <typename T>
float operator^(float, T) // where T is appropriately SFINAE'd for numeric types 

and
1
2
template <typename T>
double operator^(double, T)

and
1
2
template <typename T>
long double operator^(long double, T)

should not mean exponentiation. Why the heck would anyone XOR a float? There is precedence for using the carat symbol for exponentiation in other languages.
Right, but what about when somebody does:
a=2.0^5-1;
And expects it to work, i.e. get 31? You'd actually get 16. The problem is with the precedence, which was my point about bit-shift.
Last edited on
When in doubt, parenthesize. They are free. I almost always use parens when there are more than two terms. It makes my intentions explicit.
Ya, well, this would just be another stumbling block for newbies. Much like the fact that string s="Hi, ";s+="John!"; works, but string s="Hi, "+"John!"; does not. Many ugly things are produced because C++ attempts to be backward compatible with C. The precedence issues for << is even worse:
cout << hex << setw(2) << setfill('0') << bitfield&0xff << dec;
Its meaning is obvious, but it fails miserably. If it were up to me, I would use the += operator for output, and the -= operator for input.
iit2009154, I forgot to type the title of Item23 of more effective c++
Consider alternative libraries
Its meaning is obvious, but it fails miserably. If it were up to me, I would use the += operator for output, and the -= operator for input.


And that would solve the problem of precedence?
Yes. Assignment operators have the highest precedence. It also is a better metaphor. When you print to a file, you *add* the entry into it.
Well, not really, the scope resolution operator (::) has the greatest precedence. After that is the assignment operator.
The assignment operators actually have the lowest precedence right after the comma operator.
There has to be a reason. Let's consider:
1
2
3
cout << a += b;
// vs.
cout += a += b;


Is this possible? Are they evaluated the same?

Are there any other scenarios that stand out?
Last edited on
Rocketboy9000 wrote:
Assignment operators have the highest precedence


?????
Care to explain??
Enough debate, boys; Athar's right. Hopefully the following link will end the debate.
http://www.cppreference.com/wiki//language/operator_precedence?redirect=1

-Albatross
Oh, right, I forgot which end is "highest". Maybe "loosest" would be better.
anyway my point is that
cout += x&0xff;
would be evaluated in the obvious manner, rather than trying to and 0xff with a reference to cout.
Last edited on
Topic archived. No new replies allowed.
Pages: 12