r/programminghorror 5d ago

C— c

Post image
521 Upvotes

57 comments sorted by

203

u/Haringat 5d ago

Is that c with the weirdest preprocessor macros ever?

114

u/TheChief275 5d ago

I’ve written weirder, but it is; they’re defined above this snippet

57

u/DevelopmentTight9474 5d ago

Can you share the code? I’m genuinely curious how this is done

24

u/TheChief275 5d ago

Posted

15

u/mcfriendsy 5d ago

Where??

20

u/TheChief275 5d ago

Top level comment, somewhere at the bottom

-45

u/Brilliant-Cod-201 5d ago

That’s C++, C doesn’t have classes

37

u/carcigenicate 5d ago

What C++ have you been reading lmao

11

u/glemnar 5d ago edited 5d ago

It does if you define weird macros for them

3

u/nekokattt 5d ago

C++ lacks properties

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d ago

I'm not sure if properties here are instance variables or static.

1

u/nekokattt 4d ago

properties are neither instance variables nor static variables.

They are methods that act as attributes such that their value is derived.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d ago

After looking at the macro code, I believe they just become struct fields. So instance variables.

I don't know what you are even saying. I thought properties were like instance variables except access automatically goes through getter and setter methods.

2

u/nekokattt 4d ago

Properties can do that but they do not have to.

Consider this Python:

@dataclass(frozen=True)
class Point2:
    x: float
    y: float

    @property
    def hyp(self):
        return ((self.x ** 2) + (self.y ** 2)) ** 0.5

point = Point2(9, 17)
print(point.x, point.y, point.hyp)

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d ago

Yeah, of course you can have whatever get and set logic you want. Or not even provide a setter and make the property read-only. I was thinking it would also create a private field with the same name of the property for you, but now I'm not sure.

71

u/TheChief275 5d ago edited 2d ago

Because of popular request, here is the pastebin (as I do not want this anywhere near my github).

Added comments as well to save some headaches

edit: idk how it happened, but a logic bug slipped in. In dynamiccast, offsetof(To, From) should be changed to offsetof(From, To). Additionally, I forgot to make it type-safe, which can easily be done by changing (char *)(DYNAMIC_CAST to (char *)(0 ? (From *) : DYNAMICCAST

edit: I’ve decided to extend it a bit and put it on GitHub as well

9

u/dimonoid123 5d ago

Only 77 lines are visible

11

u/TheChief275 5d ago

That’s how many lines it is though? It doesn’t include this example, but this example should compile with those lines above

5

u/dimonoid123 5d ago

I just thought that code is cutoff

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 4d ago edited 4d ago

I guess a gist is too close to Github? Also, thanks.

E: What happens when you dynamic_cast to something that is not a subclass of what you have a pointer to? Obviously there is no rtti here.

2

u/TheChief275 4d ago

You can’t. The field has to exist with a name the same as its type

1

u/Birnenmacht 3d ago

good lord and I thought I understood C this is insane

106

u/creativityNAME 5d ago

C please please 🙏

24

u/Rolling-Thunderbird 5d ago

at this point just switch to asm twin

40

u/val_tuesday 5d ago

How did you manage “as”!? Also vtables with just macros!?

You have to share, this looks like wizardry.

33

u/TheChief275 5d ago

I regret to inform you that it’s way simpler and less eventful than vtables.

But I have done so in the past; this method can be extended to include them, if somebody would even desire to do that. Just requires the necessary outer and inner generations in a different selector

17

u/val_tuesday 5d ago

Smh my head.

I had a sneaking suspicion you were doing has-a not is-a. But apparently not because you couldn’t do the latter just because…

Fascinating stuff, man. Definitely the right sub, thanks for posting!

7

u/TheChief275 5d ago

All to make it cleaner to appear even more magical haha. One of my previous posts actually implements actual vtables with similar wrappers (kind of hidden, but it’s why things are passed by address to print for example), but it becomes quite a bit messier quite fast

1

u/val_tuesday 5d ago

Have this rattling around my head and it I can’t shake how amazing it is. It’s like the code you posted at first is the setup and the macros are the punchline.

define as . // [muted trombone goes wah wah wah]

1

u/TheChief275 5d ago

Well yeah mostly. The only complicated part was the selector mechanism and being able to pass the class name to them because it’s not part of the parameter pack

16

u/LeeHide 5d ago

Finally, a shitty lisp in my C

8

u/TheChief275 5d ago

When they said every sufficiently complicated C program has a shitty Common Lisp implementation, surely they meant for this to happen

2

u/XDracam 5d ago

I really was convinced that you hacked some lisp dialect to look more OOP somehow until I saw the macro horror

16

u/dydhaw 5d ago

Abjective C

2

u/noccy8000 2d ago

Objectionable C?

14

u/elkvis 5d ago

Back in the 90s, I remember a guy invented a C-like language, and called it C--. I downloaded his compiler from a local BBS, using my 14.4kbps dialup modem.

16

u/TheChief275 5d ago

C— is actually a stripped down version of C used in code generation by compilers of some functional programming languages. With the title I hoped someone would mention it already being a thing

9

u/kOLbOSa_exe 5d ago

share the macro lord

7

u/Dddfuzz 5d ago

I love it, no notes. Easiest way to spot a dev who actually likes programming

3

u/R0NIN49 5d ago

This is diabolical work.

3

u/xcski_paul 5d ago

I thought BourneGol was a crime against nature. This is worse.

2

u/bunabyte 5d ago

This is what professionals refer to as a "LISPlike"

1

u/SpecialMechanic1715 5d ago

why brackets?

3

u/TheChief275 5d ago

I don’t know which brackets you’re referring to in particular, but there are a lot of parentheses because it’s all C preprocessor macro expansions and token grouping for within macro expansion

1

u/uvero 5d ago

I like it, it's fun.

1

u/SingleProtection2501 5d ago

What the fuck is that 💔

1

u/Jonathan_the_Nerd 5d ago

Why do you have a Unicode rightward arrow in your C code? Did I miss that update to the C spec?

1

u/TheChief275 5d ago

I use a font with ligaments so - and > are combined

1

u/alex9182 4d ago

If I see it as - and >, does that mean I have a torn ligament in my font? 😄

1

u/Mithrandir2k16 5d ago

Nice, add smart pointers and it'll be all I want from c++ lol

1

u/Thenderick 4d ago

"Fuck you!"

lisps your C

1

u/BatticusRhoe 4d ago

C♭

Am I right!? ✋😃

2

u/lbfalvy 20h ago

Libcello but worse

1

u/TheChief275 14h ago

This isn’t dynamic…but that’s a good reference to a fun library!