r/C_Programming • • 1h ago

Is there such a problem as "C++ style C"?

C++ programmers denounce "C-style C++" that uses primitive C features over newer C++ alternatives. Is the opposite a thing in C where excessive emulation of C++ features become a problem or an antipattern? Some possible examples off my head, no idea how common they actually are or whether they are considered bad; you are welcome to add more:

  • Manual move and copy constructors on resource holders
  • Manual vtables and inheritance
  • Manual closure structs
  • Macro-based template instantiation
  • Opaque generic classes that use all of the above
6 Upvotes

21 comments sorted by

4

u/nhermosilla14 1h ago

I while ago I saw some of this in a very old codebase. It was so bad it was funny. Some things looked like OOP stuff, but without any guarantee. It ended up being worse than both regular C and C++, because you couldn't even trust the code to behave as it said it did.

1

u/Kadabrium 1h ago

GObject?

20

u/britaliope 1h ago

Macro-based template instantiation

Oh god please no.

7

u/Athropod101 1h ago

There are ways to get type info. And the __Generic macro.

It can be done.

1

u/britaliope 1h ago

Just because you can doesn't mean you should do it :'((((((

0

u/flyingron 1h ago

Yeah, believe me, we went from a bunch of awk and sed scripts and the preprocessor to code up type specific algorithms in our image processing code. It was a happy day when I finally replaced all that drek with C++ templates.

1

u/britaliope 58m ago

Honestly, awk scripts might be more maintainable than some sort of macro salad to emulate templates.

-2

u/xarop_pa_toss 54m ago

As a beginner to this world, I will run what you just wrote by an LLM and probably need a week to just skim over what all of those things are

5

u/tstanisl 1h ago

I suggest being cautious with complex  macros, they are nightmarish to debug and understand. Consider using STC (https://github.com/stclib/STC) style templates if you must as they much easier to work with. I strongly recommend using container_of pattern for inheritance. The rest is fine.

2

u/HugoNikanor 1h ago

I've seen (and written) some such C code. My usual recommendation if you're starting to implement such features, is why you aren't just writing in C++ already.

1

u/ChickenSpaceProgram 1h ago

yeah all of the above are bad, with the exception of the container_of pattern, which can sorta be considered manual inheritance, except people mostly just use it for linked lists and stuff so it's not too bad

1

u/xvillifyx 1h ago

Seems like you should just use c++ instead

1

u/SpicerXD 1h ago

GLib/GObject without Vala, lol.

1

u/qurious-crow 27m ago

Beat me to it, although GObject is more like Java-style C

1

u/Zectbumo 49m ago

Fluent Interface in C - Look ma, no this! Tuesday, November 17, 2026 by Daniel Miller https://opensource.googleblog.com/2026/11/fluent-interface-in-c-look-ma-no-this.html?m=1

1

u/d1722825 32m ago

Huge (most?) part of the Linux kernel (especially the driver model and the VFS part) is written in OOP-style C, reimplementing many functionalities of the C++ (and other OOP) language in a worse* and more obscure* way.

A good example is the container_of macro, that's basically used instead of class inheritance.

They use foo_init(struct foo*) as constructors, and function pointers in the struct as virtual functions. The ret = -EINVAL; goto ret4; error handling is very similar to destructors and (the terribly named) RAII.

* Well, when the Linux kernel began C++ was very different from the today's C++ and compilers improved a lot since then, too, so it is not fair comparison.

1

u/cblacklabel 10m ago

Manual vtables are all over the kernel (file_operations etc) and nobody complains about those.

1

u/tastygames_official 5m ago

it seems like over half of all posts in the C groups here are people trying to do C++ style C. Is it a problem? I think it is. Can you do it? Sure. But why? It's like using a screwdriver as a hammer: sure you can do it, but just use a hammer. This works both ways.

Now I peronsally see no problem doing C-like C++, as maybe you're forced into using C++ for something because of necessary libraries and instead of rewriting them to be C-compliant or expose a C ABI, you can just write C-like C++. No problems. I think that happens all the time in the world of digital audio workstation (DAW) audio plugins (CLAP, VST etc). There are some C++ libraries/frameworks to help you get a plugin up and running real quick, and I even think you MUST use C++ for one of the formats as it expects a C++ ABI. But really most plugins don't really need OOP - they just need to read input, modify it, and generate some output. Sequences of numbers representing audio.

But if you're forced to use C for something and you really just can't handle not having a fat hierarchy of classes and overloading and all that stuff - then, well - you CAN do it, but it's like using a hammer to pull out a screw. It won't be pretty and it'll take way longer than just using a screwdriver.

But I don't know of any C programmers who want C++ functionality in C. They would just use C++ then. It's really a no-brainer.

-1

u/Prefer_Diet_Soda 1h ago • Bravo

Repeat after me:

Say no to C++.

Say no to:

hidden control flow because exceptions, destructors, and operator overloads run code behind my back.

Say no to:

hidden memory behavior because std::vector, std::string, smart pointers, and references obscure allocations, moves, and lifetimes.

Say no to:

type or compile-time gymnastics because templates, auto, and constexpr turn types and build errors into unreadable puzzles.

Say no to:

structural ceremony because classes, vtables, and :: namespaces add heavy abstractions and syntax noise where plain structs, functions, and naming conventions already work.

2

u/Kadabrium 59m ago

Mallocophobia, the fear of heaps.

1

u/d1722825 14m ago

Why is foo_add(&a, b, c); is so different from a = b + c;? It's just different syntax, but both does the same. Modern IDEs even highlight overloaded operators differently, so you can clearly see it is a function call. In fact even C does very different things for a code b + c depending on eg. b being a pointer, so you can not determine what that does without knowing the types of the variables.

In C++ object lifetimes are deterministic, so you can clearly tell when, where, and in what order destructors are called.

Exceptions are a mess in C++, that is true, but the same hidden control flow exists in C, too, with setjmp/longjmp.