Skip to content

Use an inline namespace to distinguish emitted symbols by version numbers#291

Merged
ToruNiina merged 3 commits intoToruNiina:mainfrom
franzpoeschel:namespace-versioning
May 17, 2025
Merged

Use an inline namespace to distinguish emitted symbols by version numbers#291
ToruNiina merged 3 commits intoToruNiina:mainfrom
franzpoeschel:namespace-versioning

Conversation

@franzpoeschel
Copy link
Contributor

@franzpoeschel franzpoeschel commented May 16, 2025

This is a trick commonly used to ensure that header-only C++ libraries which are used only internally at build-time do not emit conflicting symbols if the library is used in multiple packages, possibly in different version numbers (see e.g. the STL).

With this PR, the symbols produced by toml11 in downstream code contain the library's current version number:

> nm -gDC libopenPMD.so | grep -i toml | head -n 10
00000000005908b8 u guard variable for toml::toml11_3_8_1::color_ansi::detail::colorize_index()::index
0000000000590888 u guard variable for toml::toml11_3_8_1::detail::comment_index<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)::index
0000000000335860 W toml::toml11_3_8_1::color_ansi::blue(std::ostream&)
0000000000335780 W toml::toml11_3_8_1::color_ansi::bold(std::ostream&)
00000000003356a0 W toml::toml11_3_8_1::color_ansi::reset(std::ostream&)
00000000003800e0 W std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > toml::toml11_3_8_1::format_key<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
0000000000336fe0 W toml::toml11_3_8_1::type_error::type_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, toml::toml11_3_8_1::source_location const&)
0000000000336fe0 W toml::toml11_3_8_1::type_error::type_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, toml::toml11_3_8_1::source_location const&)
0000000000334940 W toml::toml11_3_8_1::type_error::~type_error()
0000000000334610 W toml::toml11_3_8_1::type_error::~type_error()

This avoids conflicts when trying to link two packages that both internally use toml11 in conflicting versions.
The inline namespace ensures that application programmers do not need to change any code since the effect of inline namespaces is only on the ABI, not on the API.

Note: I will not be able to respond next week. Feel free to modify the PR as needed.

TODO:

  • Seems like I started this on the wrong branch, will need to reapply the changes
  • Fix the tests
  • examples/reflect/reflect.hpp: While this PR is generally not API-breaking, it probably does break the API for use in reflection. I could simply apply the necessary change there too, but I do not know the consequences that this would have on user code.

@franzpoeschel franzpoeschel force-pushed the namespace-versioning branch from 9ccc3ff to 5b3d75e Compare May 16, 2025 13:04
Co-authored-by: Toru Niina <niina.toru.68u@gmail.com>
@ToruNiina
Copy link
Owner

toml-test is failing due to upstream changes around new features in 1.1.0. This is not an issue of your patch. Some features that caused discussion seems to be reverted from TOML v1.1.0. I will update it later. if all other tests pass, I will merge this.

@ToruNiina
Copy link
Owner

I confirmed that example/reflect works after applying your patch. now all the todos in your comment are done. merging.

thanks!

@ToruNiina ToruNiina merged commit e4051f7 into ToruNiina:main May 17, 2025
266 of 278 checks passed
github-actions bot pushed a commit that referenced this pull request May 17, 2025
Use an inline namespace to distinguish emitted symbols by version numbers e4051f7
@franzpoeschel
Copy link
Contributor Author

Thank you too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants