News for hackers. Stuff that matters.
Re:Obscure C++ Features by bnegreve on Friday April 19, 2013 @02:13PM (#5577936)
C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value.

    if (MouseEvent *mouse = dynamic_cast<MouseEvent *>(event))
I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned.
Re:Obscure C++ Features by jomar on Friday April 19, 2013 @02:32PM (#5578024)
If this were just

    if (mouse = dynamic_cast<MouseEvent *>(event))
then it would indeed just be an if condition expression that happened to be an assignment.

But the variable is being declared as well, so what's actually going on here is a declaration and an initialiser expression. There is actually new functionality here that needed a bit of language design thought, e.g., the declared variable's scope is the whole if statement, not just the condition expression.

Re:Obscure C++ Features by kelnos on Friday April 19, 2013 @03:01PM (#5578158)
I see this as no different than being able to do

    for (int i = 0; i < len; ++i) { }
Re:Obscure C++ Features by axusgrad on Friday April 19, 2013 @04:37PM (#5578589)
Imagine my surprise, getting out of college and trying to use this at my job, when the compiler told me it was invalid! It wasn't obscure to me, but apparently was to the Sun Studio C++ compiler.
Re:Obscure C++ Features by wiml on Friday April 19, 2013 @05:18PM (#5578752)
It was added in C99, I think.
Re:Obscure C++ Features by shrughes on Friday April 19, 2013 @05:46PM (#5578856)
It was added in C++ before that.
Re:Obscure C++ Features by Ives on Friday April 19, 2013 @03:49PM (#5578372)
There are some problems with that method though.

If, for example, you'd like to reverse the condition, you can't write

    if (!(MouseEvent *mouse = dynamic_cast<MouseEvent *>(event)))
    {}
because a definition is not a general expression, but was only added as an "extra" to allow its use in some cases in conditionals.
Re:Obscure C++ Features by bnegreve on Saturday April 20, 2013 @03:58AM (#5580720)
Oh, ok I didn't know. So it is indeed a syntactical shorthand for simultaneously declaring a variable and branching.
Re:Obscure C++ Features by ben0x539 on Friday April 19, 2013 @02:31PM (#5578016)
The noteworthy bit is that declarations are valid in the conditional "expression" of if-statements and evaluate to the declared variable. It's imo not obvious that declarations are expressions sometimes.
Re:Obscure C++ Features by Sharlin on Friday April 19, 2013 @03:51PM (#5578389)
To be precise, declarations are never expressions - there are two forms of if and while statements in the grammar, one taking an expression and the other a declaration.
Re:Obscure C++ Features by Sharlin on Friday April 19, 2013 @03:49PM (#5578374)
The '=' above is not an assignment operator, as there is no assigment, it is a declaration. Yes, the following is also valid:

    MouseEvent *mouse;
    if(mouse = dynamic_cast<MouseEvent *>(event)))
but it is not the same thing.
Re:Obscure C++ Features by minasss on Friday April 19, 2013 @02:24PM (#5577985)
for example int a = b = c = 0;
Re:Obscure C++ Features by danbruc on Friday April 19, 2013 @03:21PM (#5578256)
It is more like int a = int b = int c = 0;