From 1b32a230b7e90860ca7b20ebf97435faacca28a7 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sun, 7 Sep 2025 01:20:58 +0300 Subject: [PATCH] Avoid ODR violations by using fixed types for protocol constants. At least on Linux, constants such as AF_INET, SOCK_STREAM and IPPROTO_TCP are implemented with unnamed enums, which makes these constants have different types in different translation units. This results in ODR violations as protocol_type struct ends up defined differently in every TU. Also, static_protocol that is being instantiated with these constants also produces different types in each TU. While this by itself is not an ODR violation, it may generate lots of code and data duplication. Avoid all of the above by using fixed types for family, type and protocol constants in protocol_type and static_protocol. Use int for each of the types, following POSIX socket(2) signature. Fixes https://github.com/boostorg/cobalt/issues/242. --- include/boost/cobalt/io/endpoint.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/boost/cobalt/io/endpoint.hpp b/include/boost/cobalt/io/endpoint.hpp index 1dda880f..6162bde7 100644 --- a/include/boost/cobalt/io/endpoint.hpp +++ b/include/boost/cobalt/io/endpoint.hpp @@ -50,9 +50,9 @@ struct stream_socket; struct protocol_type { - using family_t = decltype(BOOST_ASIO_OS_DEF(AF_INET)); - using type_t = decltype(BOOST_ASIO_OS_DEF(SOCK_STREAM)); - using protocol_t = decltype(BOOST_ASIO_OS_DEF(IPPROTO_TCP)); + using family_t = int; + using type_t = int; + using protocol_t = int; constexpr family_t family() const noexcept {return family_;}; constexpr type_t type() const noexcept {return type_;}; @@ -90,9 +90,9 @@ struct protocol_type protocol_t protocol_ = static_cast(0); }; -template(0), - auto Type = static_cast(0), - auto Protocol = static_cast(0)> +template(0), + protocol_type::type_t Type = static_cast(0), + protocol_type::protocol_t Protocol = static_cast(0)> struct static_protocol { using family_t = protocol_type::family_t ;