What are the different implementations of C++

Interesting question:) If I understood your goals correct, I can suggest a few solutions.

First uses template specialization, template default arguments and (of course) some macros.

Check this out:



If you use MSVC you can simplify example above eliminating and .

Trick: MSVC (2017 and maybe earlier versions) allow to omit that macros thresh, just ignoring the second declaration if they meet in the same compilation unit, like this:

BUT this will be not standard. Standard does not allow specifying default template args twice.

Meanwhile, this solution has few drawback:

  • When you include both headers, someone still can say which will use the backend specified by the first header you included. However, it would be better if compiler forced a person to specify a backend type explicitly in this circumstances, because otherwise it relies on the header inclusion order which is bad (say hello to macros).

  • Also, it assumes that both backends have the same API (like in my case)

Possible Fixes for those:

  • I am sure it is possible to make compiler give an error when both headers are included and no explicit backend was specified, but it probably involves more preprocessor things or some SFINAE...

  • If your backends do have different APIs, then you can insert a few where needed or (preferably) use C++17 if you have access to such cool features:)