Search code examples
c++c++17c++14refactoring

Is it possible to compile a project with two different language standards?


I have a project which is strictly written in C++14 and I want to use it in another project which is C++17. Some of the C++14 features like Dynamic Exception Specification were removed in C++17.

Is there any way to use and compile both code bases together? The project is large enough to make refactoring impractical.


Solution

  • This will be platform specific: It is conceivable that substantially different headers are selected according to the standard specified on the command line, etc.

    That said, here is an answer from Jonathan Wakely which assures you that with gcc there should be no such problem, by design, if you keep away from unstable features in old compilers.

    The underlying reason according to Jonathan is that C++ standard implementations in gcc, once declared stable, do not change their ABI (i.e, outward-facing type definitions, name mangling) with the selected C++ standard, not even between compiler versions.

    Because all interaction between the translation units must be restricted to the smallest common standard version there is no issue: The C++11 features do not change if you specify C++17. Newer features in the C++17 TUs cannot be used for communication with earlier-standard TUs since they were not yet available, so no issue there either. If you are able to recompile, the safest advice would be:

    1. Best to use the same std::string versions (which can be controlled from the command line at compile time).
    2. Use the same libstdc++.
    3. Use the same gcc version (and control the language standard used for each TU through the command line).

    It would make sense for other compilers to follow a similar compatibility strategy.