From 3cd29323be6d644746646302d5777bbd46553e92 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Thu, 16 Oct 2025 21:10:08 +0300 Subject: [PATCH] Add a test for boost::span verifying unqualified calls to data()/size(). The test checks that unqualified calls to data()/size() with boost::span as argument don't cause ambiguity between std:: and boost:: implementations. Fixes https://github.com/boostorg/core/issues/206. --- test/Jamfile.v2 | 1 + test/span_nonmem_data_size_test.cpp | 44 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/span_nonmem_data_size_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 3696e80..bf2e841 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -416,6 +416,7 @@ run span_constexpr_test.cpp ; run as_bytes_test.cpp ; run as_writable_bytes_test.cpp ; compile span_boost_begin_test.cpp ; +compile span_nonmem_data_size_test.cpp ; run make_span_test.cpp ; run splitmix64_test.cpp diff --git a/test/span_nonmem_data_size_test.cpp b/test/span_nonmem_data_size_test.cpp new file mode 100644 index 0000000..cc0b231 --- /dev/null +++ b/test/span_nonmem_data_size_test.cpp @@ -0,0 +1,44 @@ +/* + * Copyright Andrey Semashev 2025. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/* + * The test verifies that unqualified calls to data() and size() + * don't cause ambiguity between std:: and boost:: implementations. + * The ambiguity used to be caused by ADL bringing boost::data()/size() + * due to namespace of boost::span and a using-declaration of + * std::data()/size(). + * + * https://github.com/boostorg/core/issues/206 + */ + +#include + +#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE) + +#include +#include + +// Note: This preprocessor check should be equivalent to those in boost/core/data.hpp and boost/core/size.hpp +#if (defined(__cpp_lib_nonmember_container_access) && (__cpp_lib_nonmember_container_access >= 201411l)) || \ + (defined(_MSC_VER) && (_MSC_VER >= 1900)) + +#include +#include + +int* test_data_ambiguity(boost::span sp) +{ + using std::data; + return data(sp); +} + +boost::span::size_type test_size_ambiguity(boost::span sp) +{ + using std::size; + return size(sp); +} + +#endif // (defined(__cpp_lib_nonmember_container_access) ... +#endif // !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE)