r/ProgrammerAnimemes • • Dec 02 '22

Two hours into a new language [New Game!]

Post image
1.1k Upvotes

34 comments sorted by

103

u/hugogrant Dec 02 '22

[]<>(){ std::cout<< "o rly";}();

72

u/lord_ne Dec 03 '22

That's not even that bad. It's just a templated lambda, immediately invoked.

32

u/eXl5eQ Dec 03 '22
int i
;[i, &i](){ ;return &i; }()[&i, i];

17

u/the-ruler-of-wind Dec 03 '22

could I get an explanation on what this even is. Does this just return address of i?

25

u/eXl5eQ Dec 03 '22

[&i](){ return &i; }: a lambda that captures i and returns its' address

[&i](){ return &i; }()[i]: invoke this lambda immediately, which returns a pointer int*, then treat it as an array int[]

[i, &i] captures i twice, &i, i uses comma seperator

15

u/yymirr Dec 03 '22

No, my virgin eyes.

4

u/hekkonaay Dec 03 '22

i is uninitialized, so i believe this might segfault if you're lucky

2

u/eXl5eQ Dec 04 '22

It's UB indeed. But I don't think this will cause harm in practice cuz it doesn't access any memory address except i.

A clever compiler can always recognize this is effectively no-op, thus can be eliminated.

check on godbolt

1

u/BakuhatsuK Jan 31 '23

I'm a bit late but I think that you're effectively doing

int i;
(&i)[i];

Which is UB because you're accessing a random offset from i's location. It probably gets optimized away both because it's UB and because the value isn't used.

53

u/wqldi Dec 03 '22

Maybe you understand somethings but no one understands c++ completely

35

u/soobnar Dec 03 '22 edited Dec 03 '22

You get pulled into a false sense of security only to then immediately have something completely foreign and incomprehensible thrown your way.

9

u/hekkonaay Dec 03 '22

this is why I embrace Rust, it doesn't even let you pretend that you understand it until you actually do

4

u/DontBuyMeGoldGiveBTC Dec 03 '22

Spent hours trying to compile something simple when it came out and then gave up and went back to Javascript.

3

u/soobnar Dec 03 '22

Tbh ima stick with C/C++ until something comes along and actually kills it.

2

u/the-ruler-of-wind Dec 04 '22

carbon the project google is working on seems to be a contender but idk

4

u/soobnar Dec 04 '22

I don’t have high hopes

92

u/Faces-kun Dec 03 '22

lmao I got a degree based on it and still don’t understand it

I now use HolyC instead. No understanding needed, only faith.

5

u/MitchellMarquez42 Dec 03 '22

RIP Terry Davis

15

u/Diapolo10 Dec 03 '22

New Game! really is great

14

u/-Redstoneboi- Dec 03 '22

it's like quantum mechanics.

if you think you understand it, you don't.

17

u/masagrator Dec 03 '22

No, you don't.

8

u/HerrEisen Dec 03 '22

Finally! I know that I know nothing.

7

u/KillerRoomba13 Dec 03 '22

I think I understand it, but I still screw up pointers

1

u/[deleted] Dec 24 '22

If you're still messing with raw pointers, you haven't understood C++(11+).

2

u/ChocolateMagnateUA Jan 08 '23

You never understand C++ completely. No matter what you do, Bjarne Strousup always tells you a feature that you didn't know about. Did you know that you could use structs as the counter variable in for loops?
```

struct Counter {

int start, step, finish;

}
for (struct Counter variable = {0, 1, 100}; variable.start < stop; variable.step++) {}
```

1

u/tandonhiten Feb 09 '23

The example is wrong but, I get the sentiment

1

u/ChocolateMagnateUA Feb 09 '23

Are you sure about it?

1

u/tandonhiten Feb 09 '23

Yes.

1

u/ChocolateMagnateUA Feb 09 '23

What should I correct?

1

u/tandonhiten Feb 09 '23 edited Feb 09 '23

The condition and the increment statement. The condition should be while variable.start < variable.finish and the increment statement should be variable.start += variable.step

Edit : Also, just noticed, while defining structs, as far as I remember the name comes after the definition so

struct {
      int start, step, finish;
} Counter;

As long as I am not tripping rn...

2

u/you_do_realize Mar 16 '23

Cherish the moment, because it won't last long.

1

u/[deleted] Dec 03 '22

Suika (Alternate Universe) learns programming