I'm writing a header-only library which defines class X in header x.hpp. The implementation of X is different for different standard versions:

#if __cplusplus < 201703 class X { int a; }; #else class X { char b; short c; }; #endif

When x.hpp is included in different client translation units a.cpp and b.cpp, which are compiled with ${CXX} -std=c++11 -c a.cpp and ${CXX} -std=c++20 -c b.cpp and linked together, this silently produces a broken program since ODR violations are IF-NDR.

In practice I can use inline namespaces like so:

#if __cplusplus < 201703 inline namespace before_cpp17 { class X { int a; }; } #else inline namespace cpp17_or_later { class X { char b; short c; }; } #endif

But this only helps when X is used in the client code in a way that affects name mangling of some non-inline function (e.g. as a parameter) so that the linker fails to find the function definition. It does not help when X is used as a base or a data member of some class.

What are my options? Can I put something in x.hpp which will cause linker errors then the standard version is inconsistent regardless of how the client code uses X?

yuri kilochek's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.