back

by uecker·2y ago·view on hn ↗
C is peak C for me. I think you can better modularize in C than in C++ because with APIs build around incomplete struct types the implementation stays in the C file and does not leak into the header as with classes.
2 comments
You can use incomplete struct types in C++ as easily as in C. If you are hiding implementation, the struct will likely have a single data member

  struct private_impl_type;
  std::unique_ptr<private_impl_type> impl;
If you choose this, you trade off optimization opportunities. Your choice.
The last I checked in C++, you need to use the PIMPL (private implementation) idiom for that behavior: https://en.cppreference.com/w/cpp/language/pimpl

(not my favorite)

The C header interface is simple:

  struct foo;
  struct foo *foo_alloc();
  void foo_do_something(struct foo *p, ...);
This gives a simple to understand API with clearly defined boundary, has a stable ABI, preserves fast compilation times, avoids instruction bloat, etc. One could do this in C++ too, but what would be the point of using C++ then...