top | item 43681266

(no title)

throwaway7894 | 10 months ago

As someone who has a file with similar hacks, I will say this: I am not a C++ fan, but if you find yourself writing C code where you simulate methods via structs with function pointers often, just use C++ as a basic "C with classes" at that point. You want methods anyway, you have to go through a pointer dereference to call the function, it's just not worth the code weirdness. If you have the grit to use structs with function pointers everywhere, you have the grit to stick to the simpler subset of C++.

discuss

order

unclad5968|10 months ago

I'm torn. The step from C to any c++ is big. Now if you want anybody to be able to use your code they need to be using c++ or you have to provide a C api anyway. On the other hand, manually implementing vtables is annoying. Ive been sticking to pure C and haven't been bothered enough to go back to any c++ yet (about 6 months on my current project). I mostly only miss templated containers so far.

ryao|10 months ago

It is more annoying to want to implement an optional function in a class and then have no simple way to check if that optional function is implemented in the object without, having to edit code that guards the call sites every time you add a derived class that implements it, or having to implement your own way of querying the object to know if it is supported.

ryao|10 months ago

This is very bad advice for a few reasons:

1. It is not possible to add optional member functions (which would be pure virtual functions) to a C++ class base class and then check at runtime if they are unimplemented in the object (at least not without implementing some way to query the object, which is slow). If you say to handle this by having typeid checks at runtime, look at the VFS and then notice that you cannot implement this typeid check in advance, since you cannot add a typeid check for a derived class that did not even exist when you compiled your code. Thus, you still need to use structs of function pointers in C++. Maybe you can use C++ classes for some cases where structs of function pointers are used, but you would giving up the ability to implement optional functions in a sane way.

2. It ignores all of the things in C that are absent from C++. In particular, C++ refuses to support C’s variably modified types and variable length arrays, which are useful language features.

3. It ignores all of the things in C++ that you likely do not want, such as exceptions and RTTI. The requirement to typecast whenever you assign a void pointer to any other pointer is also ridiculous.

pjmlp|10 months ago

1. There are other approaches to this with templates and concepts, and as added bonus, stronger type checking.

Thankfully regarding 2., Google went the extra mile to pay for removing them from the Linux kernel, and they were made optional C11 onwards exactly because they are an attack vector.

3. It is called stronger type safety, ridiculous is the C community still approaching computers as if writing K&R C.

humanrebar|10 months ago

You can do everything you describe in C++. Even the language features are available (or diableable) as compilation flags.

jstimpfle|10 months ago

There's a big issue with C++ classes -- or rather methods. You need to list the entire class definition to expose public methods. That includes private members and private methods. This breaks encapsulation, exposing implementation details and their dependencies in the public header. This in turn leads to looong compile times and needless recompilations.

pjmlp|10 months ago

Hence modules, and yes I know there are still some issues to sort out.

uecker|10 months ago

Why? I do not find the syntactic sugar C++ adds very helpful and it misses other C features.

ryao|10 months ago

Perhaps he wants to jump through hoops to avoid function pointers, even when doing that in C++ for optional functions like is done in the VFS requires gymnastics:

https://godbolt.org/z/4GWdvsz6z

That is the closest I can get it to implementing an optional function via a C++ class member function instead of a function pointer. It is not only insane, but also masochistic in comparison to how it would be done via function pointers:

https://godbolt.org/z/qG3v5zcYc

codr7|10 months ago

Nope, not from my experience.

Because in C++ the features are just there right around the corner, they will seep into the code base.

And I don't want even classes, there's too much junk in there that I don't need.