Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • You may be looking for C++20 "concepts", which lets you constrain template parameters. Someone posted a great answer here to that effect, but then deleted it :( Another pattern I've seen is a "trait" template that wraps an implementation with an uniform interface – somewhat like pimpl but without the encapsulation or type erasure. But in simple cases you can just define an abstract base class from which all implementations shall inherit. Commented Dec 13, 2023 at 22:11
  • 1
    The concept answer is indeed good, it just needed modification to remove the template usage. Instead, the Mutex.hpp should include portable/Mutex.hpp and afterwards do a static_assert that the defined mutex class satisfies the concept. Commented Dec 14, 2023 at 8:32
  • 1
    However, I think OP is overthinking this issue. The platform-specific implementations aren't user extension points where you have to be paranoid about correctness. Presumably, any such code would still be in the main repository, reviewed by the original author, and compiled at least every now and then. You get lots of compile errors, you need to fix the implementation. Commented Dec 14, 2023 at 8:34
  • "not suitable for applications which do no/should not use the heap" You can use placement new to have the pimpl be a subobject of the enclosing class, although that might remove the ABI compatibility Commented Dec 14, 2023 at 9:21
  • @Caleth I had toyed with this idea in the past using memory pools (i.e., placement new). However, I'm not sure how you overcome the fact that the memory in which to perform the placement new still needs a size which you can't know exactly unless you have the declaration of the implementation. Commented Dec 14, 2023 at 15:27