Compare commits

...
Author SHA1 Message Date
Andrey Semashev b2732300e8 Updated GitHub Actions CI config.
Added new gcc and clang versions, replaced ubuntu-22.04 image
usage with containers as the image will soon be deprecated.
2026-08-14 00:12:01 +03:00
Andrey Semashev 0316628863 Reformat and simplify code. 2026-06-21 18:15:19 +03:00
Jeremy W. Murphy 286c9885d6 Constrain distance function using is_iterator type trait (#96)
* Constrain distance function using is_iterator type trait

* Add enable_if include

* Fix template syntax in distance function

* Fix syntax issue in distance enable_if

* Test which overload of distance is called

Added a test for custom distance function using Foo struct.

* Fix wrong spelling of std::ptrdiff_t

* Move Foo struct to global scope

* Andrey magic

* Fix headers, add copyright

* Constrain advance to iterators

* Test contraint of advance function with int overload

Added overload for advance function to handle integers.

* Fix enable_if condition for advance function

* Fix template syntax in advance constraint

* Fix advance test

* Remove constexpr specifier

* Make advance overload require conversion in parameter

* Explain choice of type long for n parameter
2026-06-21 18:04:10 +03:00
Andrey Semashev 4dcba468e2 Add is_*_iterator_v variable templates in C++14 and later. 2026-05-13 12:16:22 +03:00
11 changed files with 204 additions and 51 deletions
+68 -46
View File
@@ -1,4 +1,4 @@
# Copyright 2021-2025 Andrey Semashev
# Copyright 2021-2026 Andrey Semashev
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
@@ -71,12 +71,14 @@ jobs:
- g++-10
- toolset: gcc-11
cxxstd: "11,14,17,20,23"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- g++-11
- toolset: gcc-12
cxxstd: "11,14,17,20,23"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- g++-12
- toolset: gcc-13
@@ -91,18 +93,22 @@ jobs:
- g++-14
- toolset: gcc-15
cxxstd: "11,14,17,20,23,26"
os: ubuntu-latest
container: ubuntu:25.04
os: ubuntu-26.04
install:
- g++-15
- toolset: gcc-16
cxxstd: "11,14,17,20,23,26"
os: ubuntu-26.04
install:
- g++-16
- name: UBSAN
toolset: gcc-13
cxxstd: "11,14,17,20,23"
toolset: gcc-15
cxxstd: "11,14,17,20,23,26"
ubsan: 1
build_variant: debug
os: ubuntu-24.04
os: ubuntu-26.04
install:
- g++-13
- g++-15
# Linux, clang
- toolset: clang
@@ -195,7 +201,8 @@ jobs:
- toolset: clang
compiler: clang++-11
cxxstd: "11,14,17,20"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- clang-11
- g++-11
@@ -203,7 +210,8 @@ jobs:
- toolset: clang
compiler: clang++-12
cxxstd: "11,14,17,20,2b"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- clang-12
- g++-11
@@ -211,7 +219,8 @@ jobs:
- toolset: clang
compiler: clang++-13
cxxstd: "11,14,17,20,2b"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- clang-13
- g++-11
@@ -219,7 +228,8 @@ jobs:
- toolset: clang
compiler: clang++-14
cxxstd: "11,14,17,20,2b"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- clang-14
- g++-11
@@ -227,7 +237,8 @@ jobs:
- toolset: clang
compiler: clang++-15
cxxstd: "11,14,17,20,2b"
os: ubuntu-22.04
os: ubuntu-latest
container: ubuntu:22.04
install:
- clang-15
- g++-11
@@ -264,41 +275,54 @@ jobs:
- toolset: clang
compiler: clang++-20
cxxstd: "11,14,17,20,23,26"
os: ubuntu-latest
container: ubuntu:25.04
os: ubuntu-26.04
install:
- clang-20
- toolset: clang
compiler: clang++-20
compiler: clang++-21
cxxstd: "11,14,17,20,23,26"
os: ubuntu-latest
container: ubuntu:25.04
os: ubuntu-26.04
install:
- clang-20
- libc++-20-dev
- libc++abi-20-dev
- clang-21
- toolset: clang
compiler: clang++-22
cxxstd: "11,14,17,20,23,26"
os: ubuntu-26.04
install:
- clang-22
- toolset: clang
compiler: clang++-22
cxxstd: "11,14,17,20,23,26"
os: ubuntu-26.04
install:
- clang-22
- libc++-22-dev
- libc++abi-22-dev
cxxflags: -stdlib=libc++
linkflags: -stdlib=libc++
- name: UBSAN
toolset: clang
compiler: clang++-18
compiler: clang++-22
cxxstd: "11,14,17,20,23,26"
cxxflags: -stdlib=libc++
linkflags: "-stdlib=libc++ -lubsan"
ubsan: 1
build_variant: debug
os: ubuntu-24.04
os: ubuntu-26.04
install:
- clang-18
- libc++-18-dev
- libc++abi-18-dev
- clang-22
- libc++-22-dev
- libc++abi-22-dev
- toolset: clang
cxxstd: "11,14,17,20,2b"
os: macos-14
cxxstd: "11,14,17,20,2b"
- toolset: clang
cxxstd: "11,14,17,20,23"
os: macos-15
cxxstd: "11,14,17,20,23,26"
- toolset: clang
os: macos-26
cxxstd: "11,14,17,20,23,26"
- name: CMake tests
cmake_tests: 1
@@ -434,6 +458,7 @@ jobs:
BUILD_JOBS=$((nproc || sysctl -n hw.ncpu) 2> /dev/null)
echo "BUILD_JOBS=$BUILD_JOBS" >> $GITHUB_ENV
echo "CMAKE_BUILD_PARALLEL_LEVEL=$BUILD_JOBS" >> $GITHUB_ENV
echo BUILD_JOBS: $BUILD_JOBS
DEPINST_ARGS=()
GIT_VERSION="$(git --version | sed -e 's/git version //')"
GIT_HAS_JOBS=1
@@ -485,25 +510,22 @@ jobs:
git submodule update --init tools/boostdep
DEPINST_ARGS+=("$LIBRARY")
python tools/boostdep/depinst/depinst.py "${DEPINST_ARGS[@]}"
if [ -z "${{matrix.cmake_tests}}" ]
then
./bootstrap.sh
./b2 headers
if [ -n "${{matrix.compiler}}" -o -n "$GCC_TOOLCHAIN_ROOT" ]
then
echo -n "using ${{matrix.toolset}} : : ${{matrix.compiler}}" > ~/user-config.jam
if [ -n "$GCC_TOOLCHAIN_ROOT" ]
then
echo -n " : <compileflags>\"--gcc-toolchain=$GCC_TOOLCHAIN_ROOT\" <linkflags>\"--gcc-toolchain=$GCC_TOOLCHAIN_ROOT\"" >> ~/user-config.jam
fi
echo " ;" >> ~/user-config.jam
fi
fi
- name: Run tests
if: matrix.cmake_tests == ''
run: |
cd boost-root
./bootstrap.sh
./b2 headers
if [ -n "${{matrix.compiler}}" -o -n "$GCC_TOOLCHAIN_ROOT" ]
then
echo -n "using ${{matrix.toolset}} : : ${{matrix.compiler}}" > ~/user-config.jam
if [ -n "$GCC_TOOLCHAIN_ROOT" ]
then
echo -n " : <compileflags>\"--gcc-toolchain=$GCC_TOOLCHAIN_ROOT\" <linkflags>\"--gcc-toolchain=$GCC_TOOLCHAIN_ROOT\"" >> ~/user-config.jam
fi
echo " ;" >> ~/user-config.jam
fi
B2_ARGS=("-j" "$BUILD_JOBS" "toolset=${{matrix.toolset}}" "cxxstd=${{matrix.cxxstd}}")
if [ -n "${{matrix.build_variant}}" ]
then
@@ -518,7 +540,7 @@ jobs:
if [ -n "${{matrix.ubsan}}" ]
then
export UBSAN_OPTIONS="print_stacktrace=1"
B2_ARGS+=("cxxflags=-fsanitize=undefined -fno-sanitize-recover=undefined" "linkflags=-fsanitize=undefined -fuse-ld=gold" "define=UBSAN=1" "debug-symbols=on" "visibility=global")
B2_ARGS+=("cxxflags=-fsanitize=undefined -fno-sanitize-recover=undefined" "linkflags=-fsanitize=undefined" "define=UBSAN=1" "debug-symbols=on" "visibility=global")
fi
if [ -n "${{matrix.cxxflags}}" ]
then
@@ -605,12 +627,12 @@ jobs:
rmdir /s /q "..\snapshot"
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs %GIT_FETCH_JOBS%" %LIBRARY%
cmd /c bootstrap
b2 -d0 headers
- name: Run tests
run: |
cd boost-root
cmd /c bootstrap
b2 -d0 headers
if not "${{matrix.cxxstd}}" == "" set CXXSTD=cxxstd=${{matrix.cxxstd}}
if not "${{matrix.addrmd}}" == "" set ADDRMD=address-model=${{matrix.addrmd}}
if not "${{matrix.build_variant}}" == "" (set BUILD_VARIANT=variant=${{matrix.build_variant}}) else (set BUILD_VARIANT=variant=%DEFAULT_BUILD_VARIANT%)
+5 -1
View File
@@ -7,7 +7,10 @@
#ifndef BOOST_ITERATOR_ADVANCE_HPP
#define BOOST_ITERATOR_ADVANCE_HPP
#include <type_traits>
#include <boost/config.hpp>
#include <boost/iterator/is_iterator.hpp>
#include <boost/iterator/iterator_categories.hpp>
namespace boost {
@@ -56,7 +59,8 @@ inline BOOST_CXX14_CONSTEXPR void advance_impl(RandomAccessIterator& it, Distanc
namespace advance_adl_barrier {
template< typename InputIterator, typename Distance >
inline BOOST_CXX14_CONSTEXPR void advance(InputIterator& it, Distance n)
inline BOOST_CXX14_CONSTEXPR typename std::enable_if< is_iterator< InputIterator >::value >::type
advance(InputIterator& it, Distance n)
{
detail::advance_impl(it, n, typename iterator_traversal< InputIterator >::type());
}
+8 -2
View File
@@ -1,4 +1,5 @@
// Copyright (C) 2017 Michel Morin.
// Copyright (C) 2026 Jeremy W. Murphy
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -7,7 +8,10 @@
#ifndef BOOST_ITERATOR_DISTANCE_HPP
#define BOOST_ITERATOR_DISTANCE_HPP
#include <type_traits>
#include <boost/config.hpp>
#include <boost/iterator/is_iterator.hpp>
#include <boost/iterator/iterator_categories.hpp>
#include <boost/iterator/iterator_traits.hpp>
@@ -40,8 +44,10 @@ distance_impl(RandomAccessIterator first, RandomAccessIterator last, random_acce
namespace distance_adl_barrier {
template< typename SinglePassIterator >
inline BOOST_CXX14_CONSTEXPR typename iterator_difference< SinglePassIterator >::type
distance(SinglePassIterator first, SinglePassIterator last)
inline BOOST_CXX14_CONSTEXPR typename std::enable_if<
is_iterator< SinglePassIterator >::value,
iterator_difference< SinglePassIterator >
>::type::type distance(SinglePassIterator first, SinglePassIterator last)
{
return detail::distance_impl(first, last, typename iterator_traversal< SinglePassIterator >::type());
}
+9
View File
@@ -139,10 +139,19 @@ struct is_iterator< volatile T > : public detail::is_iterator_impl< T >::type {}
template< typename T >
struct is_iterator< const volatile T > : public detail::is_iterator_impl< T >::type {};
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
template< typename T >
BOOST_INLINE_VARIABLE constexpr bool is_iterator_v = iterators::is_iterator< T >::value;
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
} // namespace iterators
using iterators::is_iterator;
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
using iterators::is_iterator_v;
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
} // namespace boost
#endif // BOOST_ITERATOR_IS_ITERATOR_HPP_INCLUDED_
@@ -4,6 +4,7 @@
#ifndef IS_LVALUE_ITERATOR_DWA2003112_HPP
#define IS_LVALUE_ITERATOR_DWA2003112_HPP
#include <boost/config.hpp>
#include <boost/iterator/detail/type_traits/conjunction.hpp>
#include <iterator>
@@ -71,11 +72,23 @@ struct is_non_const_lvalue_iterator :
{
};
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
template< typename T >
BOOST_INLINE_VARIABLE constexpr bool is_lvalue_iterator_v = iterators::is_lvalue_iterator< T >::value;
template< typename T >
BOOST_INLINE_VARIABLE constexpr bool is_non_const_lvalue_iterator_v = iterators::is_non_const_lvalue_iterator< T >::value;
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
} // namespace iterators
using iterators::is_lvalue_iterator;
using iterators::is_non_const_lvalue_iterator;
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
using iterators::is_lvalue_iterator_v;
using iterators::is_non_const_lvalue_iterator_v;
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
} // namespace boost
#endif // IS_LVALUE_ITERATOR_DWA2003112_HPP
@@ -6,6 +6,7 @@
#include <iterator>
#include <type_traits>
#include <boost/config.hpp>
namespace boost {
namespace iterators {
@@ -60,10 +61,19 @@ struct is_readable_iterator :
{
};
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
template< typename T >
BOOST_INLINE_VARIABLE constexpr bool is_readable_iterator_v = iterators::is_readable_iterator< T >::value;
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
} // namespace iterators
using iterators::is_readable_iterator;
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
using iterators::is_readable_iterator_v;
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
} // namespace boost
#endif // IS_READABLE_ITERATOR_DWA2003112_HPP
+21
View File
@@ -20,6 +20,20 @@ void test_advance(Iterator it_from, Iterator it_to, int n)
BOOST_TEST(it_from == it_to);
}
// Definitely not an iterator
struct Foo
{
int x = 0;
// Don't use type "int" for "n", otherwise it matches literal int exactly
// and doesn't demonstrate the effect of enable_if.
friend
void advance(Foo &value, long n)
{
value.x += 10 * n;
}
};
int main()
{
int array[3] = {1, 2, 3};
@@ -87,5 +101,12 @@ int main()
);
}
{
using boost::advance;
Foo bar;
advance(bar, 3);
BOOST_TEST(bar.x == 30);
}
return boost::report_errors();
}
+14
View File
@@ -4,6 +4,7 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstddef>
#include <vector>
#include <list>
#include <boost/container/slist.hpp>
@@ -19,6 +20,13 @@ void test_distance(Iterator it_from, Iterator it_to, int n)
BOOST_TEST(boost::distance(it_from, it_to) == n);
}
// Definitely not an iterator.
struct Foo
{
constexpr friend
std::ptrdiff_t distance(Foo const &, Foo const &) { return -1; }
};
int main()
{
int array[3] = {1, 2, 3};
@@ -80,5 +88,11 @@ int main()
);
}
{
// Make boost::distance visible since we're not actually in the boost namespace here.
using boost::distance;
auto result = distance(Foo{}, Foo{});
BOOST_TEST(result == -1);
}
return boost::report_errors();
}
+21
View File
@@ -17,6 +17,7 @@
#include <vector>
#include <string>
#include <iterator>
#include <boost/config.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/core/lightweight_test.hpp>
@@ -160,5 +161,25 @@ int main()
BOOST_TEST(!boost::iterators::is_iterator< int (&)(int) >::value);
BOOST_TEST(!boost::iterators::is_iterator< int (&)[10] >::value);
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
BOOST_TEST(boost::iterators::is_iterator_v< int* >);
BOOST_TEST(boost::iterators::is_iterator_v< const int* >);
BOOST_TEST(boost::iterators::is_iterator_v< complete* >);
BOOST_TEST(boost::iterators::is_iterator_v< std::reverse_iterator< int* > >);
BOOST_TEST(boost::iterators::is_iterator_v< std::reverse_iterator< complete* > >);
BOOST_TEST(boost::iterators::is_iterator_v< adapted_iterator< int* > >);
BOOST_TEST(!boost::iterators::is_iterator_v< int >);
BOOST_TEST(!boost::iterators::is_iterator_v< void* >);
BOOST_TEST(!boost::iterators::is_iterator_v< int (int) >);
BOOST_TEST(!boost::iterators::is_iterator_v< int (*)(int) >);
BOOST_TEST(!boost::iterators::is_iterator_v< int complete::* >);
BOOST_TEST(!boost::iterators::is_iterator_v< int (complete::*)(int) >);
BOOST_TEST(!boost::iterators::is_iterator_v< int*[] >);
BOOST_TEST(!boost::iterators::is_iterator_v< int*[10] >);
BOOST_TEST(!boost::iterators::is_iterator_v< int& >);
BOOST_TEST(!boost::iterators::is_iterator_v< int*& >);
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
return boost::report_errors();
}
+22 -1
View File
@@ -2,11 +2,12 @@
// subject to 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)
#include <boost/iterator/is_lvalue_iterator.hpp>
#include <deque>
#include <iterator>
#include <cstddef> // std::ptrdiff_t
#include <boost/config.hpp>
#include <boost/noncopyable.hpp>
#include <boost/iterator/is_lvalue_iterator.hpp>
struct v
{
@@ -168,5 +169,25 @@ int main()
static_assert(!boost::is_non_const_lvalue_iterator<constant_lvalue_iterator<float>>::value,
"boost::is_non_const_lvalue_iterator<constant_lvalue_iterator<float>>::value is expected to be false.");
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
static_assert(boost::is_lvalue_iterator_v<v*>,
"boost::is_lvalue_iterator_v<v*> is expected to be true.");
static_assert(boost::is_lvalue_iterator_v<v const*>,
"boost::is_lvalue_iterator_v<v const*> is expected to be true.");
static_assert(!boost::is_lvalue_iterator_v<proxy_iterator<int>>,
"boost::is_lvalue_iterator_v<proxy_iterator<int>> is expected to be false.");
static_assert(!boost::is_lvalue_iterator_v<value_iterator>,
"boost::is_lvalue_iterator_v<value_iterator> is expected to be false.");
static_assert(boost::is_non_const_lvalue_iterator_v<v*>,
"boost::is_non_const_lvalue_iterator_v<v*> is expected to be true.");
static_assert(!boost::is_non_const_lvalue_iterator_v<v const*>,
"boost::is_non_const_lvalue_iterator_v<v const*> is expected to be false.");
static_assert(!boost::is_non_const_lvalue_iterator_v<proxy_iterator<int>>,
"boost::is_non_const_lvalue_iterator_v<proxy_iterator<int>> is expected to be false.");
static_assert(!boost::is_non_const_lvalue_iterator_v<value_iterator>,
"boost::is_non_const_lvalue_iterator_v<value_iterator> is expected to be false.");
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
return 0;
}
+13 -1
View File
@@ -2,11 +2,12 @@
// subject to 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)
#include <boost/iterator/is_readable_iterator.hpp>
#include <deque>
#include <iterator>
#include <cstddef> // std::ptrdiff_t
#include <boost/config.hpp>
#include <boost/noncopyable.hpp>
#include <boost/iterator/is_readable_iterator.hpp>
struct v
{
@@ -97,5 +98,16 @@ int main()
static_assert(boost::is_readable_iterator<noncopyable_iterator>::value,
"boost::is_readable_iterator<noncopyable_iterator>::value is expected to be true.");
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
static_assert(boost::is_readable_iterator_v<v*>,
"boost::is_readable_iterator_v<v*> is expected to be true.");
static_assert(boost::is_readable_iterator_v<v const*>,
"boost::is_readable_iterator_v<v const*> is expected to be true.");
static_assert(boost::is_readable_iterator_v<proxy_iterator>,
"boost::is_readable_iterator_v<proxy_iterator> is expected to be true.");
static_assert(!boost::is_readable_iterator_v<proxy_iterator2>,
"boost::is_readable_iterator_v<proxy_iterator2> is expected to be false.");
#endif // !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
return 0;
}