Live data from Hacker News

Obscure C++ Features

madebyevan.com

41–50 of 58 posts

Re: Obscure C++ Features

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

Except you didn't have to learn it as part of learning C, you didn't have to learn C to learn C++, and I would bet 99% of C and C++ programmers have no idea that 5[p] is valid code.

Re: Obscure C++ Features

#43
post #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).

That's because operator overloading in C++ isn't commutative in general. For integers, 'a + b' is the same as 'b + a', but it's not the same in C++.

For example, I could do:

    class MyClass {int operator+(int a) { return a; }};

    MyClass x;
    x + 1;  // Valid.
    1 + x;  // Type error.
EDIT: My mistake. C++ requires these operators to be nonstatic member functions:

     = -> [] ()
Which, annoyingly enough, means that you can define '+' over types that you haven't implemented, but you can't define [] or =.

Re: Obscure C++ Features

#44
post #25

Earlier quoted context omitted.

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.

It was added in C99, I think.

Re: Obscure C++ Features

#45
post #44

Earlier quoted context omitted.

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.

It was added in C99, I think.

It was added in C++ before that.

Re: Obscure C++ Features

#47

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

If there is ever an IOC++CC (International Obfuscated C++ Code Contest), I imagine that this technique will show up all over the place.

Bonus: Can you paste together the "compl" token with preprocessor macros?

Re: Obscure C++ Features

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

While it's definitely a rarely-used feature, the equivalence a[b] == *(a + b) == b[a] is fundamental to pointer math. Once you grok pointer math, then this is an interesting bit of syntax trivia and should not be forgotten easily.

Re: Obscure C++ Features

#50

Dark font on dark background. Unreadable.

Don't the mainstream browsers support custom styling yet? Like say, a high contrast style?

In Opera, "Page > Style > User Mode" does the trick. I know FF and IE have similar functionality, but I don't know the shortcut.
Post reply on HN