How do people deal with the problem of creating portable public interfaces?

So far the best solution I've found is the "pimpl"/"cheshire cat" pattern, but it's cumbersome and sometimes problematic. Isn't there a better way of doing this or is just one of those neat little things that we all get to struggle with?

I want to have a sort of 'wall of implementation' that separates the portable side of my code from the system-dependent side. Apart from forward declaring a struct for the private members (pimpl pattern) all I can think of (and this makes me very afraid) is to have the 'real' class inherit from a virtual base that only has the public members, then a factory function could instantiate the derived class and return a pointer to the base (Would that result in clipping when it's deleted? I really don't want to do it like this. ).

Anyway, I was just curious what methods other people use for this kind of thing. Any advice would be greatly appreciated.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Not if you make the destructor virtual. You can also make the destructor non-public and require an explicit cleanup function instead of delete. This can be either a non-member function or a member function like COM's Release().

How do people deal with the problem of creating portable public interfaces?

So far the best solution I've found is the "pimpl"/"cheshire cat" pattern, but it's cumbersome and sometimes problematic. Isn't there a better way of doing this or is just one of those neat little things that we all get to struggle with?

I want to have a sort of 'wall of implementation' that separates the portable side of my code from the system-dependent side.

I'm not 100% sure I'm interpreting your question correctly, but I do this alot:

Then have one .cpp file for each platform. Each platform will typically have some macro whose existence you can rely on to differentiate it from others. For example _WIN32 is defined supported by every Windows compiler I've ever encountered.

Your Linux code is "protected" from your Windows code (for example), because the Linux side won't even know the Windows stuff is there, and vice versa.

You can still use pimpl if you want, but if you wouldn't have used pimpl if there was only one platform to consider, then there's little point in doing so here.

The only problem with that approach, is that anyone who wants to use a fish, has to include the windows-specific headers that define HANDLE and LARGE_INTEGER. Ideally, only your win32 cpp file should have to include those headers.

I've see one SDK get around this with crap like:

#ifdef PLATFORM_BLAH
char members[48];
#else
...

In the past I've worked around this by making foo.h only contain the public interface (to be included by people who want to use the class), then foo_impl.h contain the members (to be included by people who want to instantiate the class), and foo_[platform].cpp with the implementations.

Include the windows-specific headers that define HANDLE and LARGE_INTEGER. Ideally, only your win32 cpp file should have to include those headers.

Yes, practically speaking, I do sometimes end up pimpl-ing the windows implementation for that reason, or relying on the fact that I could use void* instead of HANDLE, __int64 instead of LARGE_INTEGER, etc.

I actually have a class called 'interface' that overloads new/delete/new[]/etc... and makes the dtor virtual.
(In debug build my new operator takes the file & line of the allocation and keeps track of it in a map so at exit I instantly see leaks and where they came form.)

If it's OOD code anyway then you don't really need pImpl. If the code is high-level the OOD overhead is fine. If it's low-level you don't want the overhead of either.

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara