Live data from Hacker News

Obscure C++ Features

madebyevan.com

31–40 of 58 posts

Re: Obscure C++ Features

#31
None of these things are obscure. At all. It was an immediate fail to begin with an explanation of indexing as pointer arithmetic. This is known to any C or C++ programmer beyond an absolute beginner level.

Re: Obscure C++ Features

#32

C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value. if (MouseEvent *mouse = dynamic_cast (event)) I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned.

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(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

#33

C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value. if (MouseEvent *mouse = dynamic_cast (event)) I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned.

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(event)))
but it is not the same thing.

Re: Obscure C++ Features

#34

C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value. if (MouseEvent *mouse = dynamic_cast (event)) I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned.

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.

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

#35
post #29
post #19

Earlier quoted context omitted.

The point was probably that it is not obscure; it's what you had to learn when using plain C in the first week.

And the next week, and six months after that, again.

Seriously? It's not a difficult concept to understand...

Re: Obscure C++ Features

#36

Re. the "redefining keywords" trick. I know this works with at least some versions of g++. However when I tried doing it in MSVC 2008 I ended up with link errors between the library object files (compiled without the redefinition trick) and the app executable that did use the trick. It looked like MSVC was encoding the visibility in the mangled method names.

If I remember correctly, it's undefined behavior if you redefine keywords anywhere, not just for library headers as the article suggests.

Re: Obscure C++ Features

#38
post #19
post #15

Earlier quoted context omitted.

>The behavior of [] is from C -- it's not a bizarro C++ feature. I'd say that a feature of C++ is still a feature of C++ even if the same feature is in C.

The point was probably that it is not obscure; it's what you had to learn when using plain C in the first week.

calling a[5] isn't obscure, but 5[a] sure is!

Re: Obscure C++ Features

#39
post #25
post #21

Earlier quoted context omitted.

If this were just if (mouse = dynamic_cast (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, no…

I see this as no different than being able to do for (int i = 0; i

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.
Post reply on HN