Live data from Hacker News

Obscure C++ Features

madebyevan.com

21–30 of 58 posts

Re: Obscure C++ Features

#21

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.

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, not just the condition expression.

Re: Obscure C++ Features

#22
My favorite is that these are indeed "alternate operator tokens" an so you can use `compl` in place of ~ to name a destructor. Like, for explicit destruction:

    MyType* foo = ...;
    foo->compl MyType();

Re: Obscure C++ Features

#23

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.

[deleted]

Re: Obscure C++ Features

#24

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.

[deleted]

Re: Obscure C++ Features

#25
post #21

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.

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 

Re: Obscure C++ Features

#26
post #8

The behavior of [] is from C -- it's not a bizarro C++ feature. The "redefining keywords" is the pre-processor. That's what it does. There isn't anything magic here. Strictly speaking it can be run entirely ahead of the C/C++ compiler, on any file you want. There's no requirement that it is C/C++ - you could use it on a python script (as long as you weren't using # comments anywhere :D )

Moreover, 3[a] appears to be supported solely for backwards compatibility with C since it works only for pointers - you can't do this with classes which define operator[](int).

Re: Obscure C++ Features

#27
I think the only thing "obscure" in this list is the "pointer-to-member" syntax. It's conceptually simple but the syntax is pretty bizarre, hard to remember even when you're searching for it, and probably a lot of C++ programmers working professionally will not recognize it.

I remember a few years ago I was writing some code where using pointer-to-struct-member made lots of sense to me. Doing a code review I was asked politely by a peer to rewrite without them. I disagreed but didn't mind doing so, if it made people on the team happier about readability.

Re: Obscure C++ Features

#28
post #16

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.

for example int a = b = c = 0;

It is more like int a = int b = int c = 0;

Re: Obscure C++ Features

#29
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.

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

Re: Obscure C++ Features

#30
post #9
post #8

The behavior of [] is from C -- it's not a bizarro C++ feature. The "redefining keywords" is the pre-processor. That's what it does. There isn't anything magic here. Strictly speaking it can be run entirely ahead of the C/C++ compiler, on any file you want. There's no requirement that it is C/C++ - you could use it on a python script (as long as you weren't using # comments anywhere :D )

Definitions of obscure: 1. Not easily distinguished 2. Not readily understandable/expressable 3. Not commonly known I didn't see the author use the word "bizarro" anywhere, nor make any claims about any of this stuff being weird. He actually presented almost everything in a fairly neutral tone, except when he said that using the preprocessor to override keywords was dangerous (which could be construed as opinion).

It's not an opinion, it's forbidden by the language specification so it's not portable.
Post reply on HN