15

This compiles and works in C++:

int arr[10];

5[arr] = 42;

cout << 5[arr] << endl;

I know why it works technically. Its just pointer math. But I have to ask "Why?" Of all that is unholy, WHY?!

Comments
  • 12
    The question is why /doesn’t/ that work in java, JavaScript, c# and any other piece of garbage?

    Answer: they took something that /should/ be as simple as basic pointer math and made it 1000x more complicated than that, with all kinds of type information checking and callbacks etc etc...
  • 3
    @FrodoSwaggins because people like not being retarded
  • 5
    @tekashi IMO over complicating things is retarded. C is not over complicated
  • 6
    Referring to the original C++: at the end of the day, it's just math. Some silly math trickery, but math nontheless.
    We don't really think of it that way because of words and names and abstractions (such as language), but that doesn't change its fundamental nature.
    And if math doesn't work in the world of programming, then what does? :)

    (Any incidental reference to Javascript is purely voluntary and intentional. Fuck Javascript with a rusty pole)
  • 5
    The question is not why it works.
    The question is "why it is allowed"?
    What do you gain to have that in a language?
  • 11
    @Pickman The question of "why is it allowed?" infers that there should be a list of disallowed yet completely functional things. Why arbitrarily restrict the developer? Who curates this list? Are they more knowledgeable and skilled than I? Says who? Do their arbitrary rules even make sense for my code and architecture? The compiler should never think it knows better than the programmer, and should never limit their freedom to write perfectly valid (or even questionable) code.

    To blatantly simplify your question, you are asking for compilers to censor code. Fuck censorship of all kinds, and everyone who insists on it.
  • 7
    @Root but c++ does not make everything legal. That's Brainfuck.
    In reality the compiler does indeed know better.
    You're mistaking syntactical flexibility for expressiveness of a language.
    If in c++ you couldn't write 5[arr] you would not lose anything but the chance to write confusing code.
    The compiler could easily generate the same code from 5[arr] and arr[5] and you would never notice. At this point why not allow 5[arr]1 to be interpreted as arr[5]+1? I'm sure that would spare me some time. If you want your code to be readable you have to make some sacrifices like writing that plus or putting the pointer outside of the brackets like everyone expects you to. Mind you, nothing wrong with unreadable code from a functional point of view. But writing it like that is being a bit antisocial. Specifically in this case it is to randomly speak like Yoda.
  • 3
    Of course for C, it's because it's just arithmetic. For C++, it's because you can asymmetrically overload operator[].

    https://godbolt.org/z/jK5jNq
  • 3
    (Please don't do this.)
  • 3
    You just answered your question "it's just pointer math"
  • 3
    @root while I agree that you should be allowed to do anything, the compiler should also at least warn you if you try to do something stupid. That or there should be checks in place that make you jump through hoops to do stupid things, because where the majority of programmers are concerned the people that designed the compiler generally do know best and that warning may just say you from a major headache and hours of trying to track down a stupid bug.
  • 3
    @Pickman c++ doesn’t make everything legal but there are very obvious reasons for it, and of you’ve ever worked on a compiler you’d quickly understand all of them. There’s nothing hazardous about the example we’re discussing.
  • 1
    @root It has already been said, but I strongly disagree with a language that allow you to make anything because it can.

    You make code for a result result so yes, who care how it process internally? But to ensure it work well, to improve it, or do any modification: you HAVE to read the code and more over understand it. Tricks are sometimes cool and handy, but in most case I like if there is a single and clear way to do something. If by reading the global structure of a code you can know way it should do because everyone make it the same way you win everything. Also learning the language became simpler and thus cause less error.

    However if you have to read every single character while predicting in your mind all the edge cases to get an overview because someone (or yourself) use lots of weird tricks, you will only waste your time and on top of that probably miss bugs.

    Censoring what you can do is ad, but restricting how you can write it in order you ensure readability is not.
  • 2
    @Kasonnara yeah but when I think of languages that let you do /anything/ especially dangerous things that make no sense I think of JavaScript python and c#. C++ is way more limited and precise than all of those examples, but that’s a good thing. This example is just not a big deal at all, but reflection and weak typing are a deal breaker.
  • 0
    @Kasonnara that's what custom (DE level) error checking rules are for.
  • 0
    @FrodoSwaggins I totally agree. Removing its legality does not however diminish the expressiveness of the language.
    Considering that the de facto standard is different allowing this expose you to the temptation of writing code that is hard to read for a beginner.
    Of course c++ is rather safe in that regard.
Your Job Suck?
Get a Better Job
Add Comment