[DEV] add v1.76.0

This commit is contained in:
2021-10-05 21:37:46 +02:00
parent a97e9ae7d4
commit d0115b733d
45133 changed files with 4744437 additions and 1026325 deletions

View File

@@ -0,0 +1,79 @@
# Copyright (C) 2019 T. Zachary Laine
#
# 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)
cmake_minimum_required(VERSION 3.5)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
project(stl_interfaces)
##################################################
# C++ standard version selection
##################################################
set(CXX_STD 14 CACHE STRING "Set to X to enable C++X builds.")
message("-- Using -std=c++${CXX_STD}")
##################################################
# Sanitizers
##################################################
set(USE_ASAN false CACHE BOOL "Set to true to enable -fsanitize=address when building tests.")
set(USE_UBSAN false CACHE BOOL "Set to true to enable -fsanitize=undefined when building tests.")
if (USE_ASAN AND USE_UBSAN)
message(FATAL_ERROR "USE_ASAN and USE_UBSAN must not be enabled at the same time")
elseif (USE_ASAN)
set(compile_flags -fsanitize=address)
set(link_flags -fsanitize=address)
message("-- Using -fsanitize=address")
elseif (USE_UBSAN)
set(compile_flags -fsanitize=undefined)
set(link_flags -fsanitize=undefined)
message("-- Using -fsanitize=undefined")
endif()
##################################################
# Code coverage
##################################################
if (UNIX)
set(BUILD_COVERAGE false CACHE BOOL "Set to true to enable code coverage when building tests. Only Linux and Mac are supported.")
if (BUILD_COVERAGE)
message("-- Building for code coverage; disabling any sanitizers")
if (APPLE)
set(compile_flags -fprofile-arcs -ftest-coverage)
set(CMAKE_BUILD_TYPE Debug)
set(link_flags --coverage)
else ()
set(compile_flags --coverage)
set(CMAKE_BUILD_TYPE Debug)
set(link_flags --coverage)
endif ()
endif ()
endif ()
##################################################
# Dependencies
##################################################
set(boost_components)
include(dependencies)
##################################################
# stl_interfaces library
##################################################
add_library(stl_interfaces INTERFACE)
target_include_directories(stl_interfaces INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(stl_interfaces INTERFACE boost)
if (link_flags)
target_link_libraries(stl_interfaces INTERFACE ${link_flags})
target_compile_options(stl_interfaces INTERFACE ${compile_flags})
endif ()
if (NOT MSVC)
target_compile_options(stl_interfaces INTERFACE -Wall)
endif ()
add_subdirectory(test)
add_subdirectory(example)

View File

@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,199 @@
# stl_interfaces
An updated C++20-friendly version of the `iterator_facade` and
`iterator_adaptor` parts of Boost.Iterator (now called `iterator_interface`);
a pre-C++20 version of C++20's `view_interface`; and a new template called
`container_interface`, meant to aid the creation of new containers; all
targeting standardization. This library requires at least C++14.
For the iterator portion -- if you need to write an iterator, `iterator_interface` turns this:
```c++
struct repeated_chars_iterator
{
using value_type = char;
using difference_type = std::ptrdiff_t;
using pointer = char const *;
using reference = char const;
using iterator_category = std::random_access_iterator_tag;
constexpr repeated_chars_iterator() noexcept :
first_(nullptr),
size_(0),
n_(0)
{}
constexpr repeated_chars_iterator(
char const * first,
difference_type size,
difference_type n) noexcept :
first_(first),
size_(size),
n_(n)
{}
constexpr reference operator*() const noexcept
{
return first_[n_ % size_];
}
constexpr value_type operator[](difference_type n) const noexcept
{
return first_[(n_ + n) % size_];
}
constexpr repeated_chars_iterator & operator++() noexcept
{
++n_;
return *this;
}
constexpr repeated_chars_iterator operator++(int)noexcept
{
repeated_chars_iterator retval = *this;
++*this;
return retval;
}
constexpr repeated_chars_iterator & operator+=(difference_type n) noexcept
{
n_ += n;
return *this;
}
constexpr repeated_chars_iterator & operator--() noexcept
{
--n_;
return *this;
}
constexpr repeated_chars_iterator operator--(int)noexcept
{
repeated_chars_iterator retval = *this;
--*this;
return retval;
}
constexpr repeated_chars_iterator & operator-=(difference_type n) noexcept
{
n_ -= n;
return *this;
}
friend constexpr bool operator==(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs.first_ == rhs.first_ && lhs.n_ == rhs.n_;
}
friend constexpr bool operator!=(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return !(lhs == rhs);
}
friend constexpr bool operator<(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs.first_ == rhs.first_ && lhs.n_ < rhs.n_;
}
friend constexpr bool operator<=(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs == rhs || lhs < rhs;
}
friend constexpr bool operator>(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return rhs < lhs;
}
friend constexpr bool operator>=(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return rhs <= lhs;
}
friend constexpr repeated_chars_iterator
operator+(repeated_chars_iterator lhs, difference_type rhs) noexcept
{
return lhs += rhs;
}
friend constexpr repeated_chars_iterator
operator+(difference_type lhs, repeated_chars_iterator rhs) noexcept
{
return rhs += lhs;
}
friend constexpr repeated_chars_iterator
operator-(repeated_chars_iterator lhs, difference_type rhs) noexcept
{
return lhs -= rhs;
}
friend constexpr difference_type operator-(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs.n_ - rhs.n_;
}
private:
char const * first_;
difference_type size_;
difference_type n_;
};
```
into this:
```c++
struct repeated_chars_iterator : boost::stl_interfaces::iterator_interface<
repeated_chars_iterator,
std::random_access_iterator_tag,
char,
char>
{
constexpr repeated_chars_iterator() noexcept :
first_(nullptr),
size_(0),
n_(0)
{}
constexpr repeated_chars_iterator(
char const * first, difference_type size, difference_type n) noexcept :
first_(first),
size_(size),
n_(n)
{}
constexpr char operator*() const noexcept { return first_[n_ % size_]; }
constexpr repeated_chars_iterator & operator+=(std::ptrdiff_t i) noexcept
{
n_ += i;
return *this;
}
constexpr auto operator-(repeated_chars_iterator other) const noexcept
{
return n_ - other.n_;
}
private:
char const * first_;
difference_type size_;
difference_type n_;
};
```
The code size savings are even more dramatic for `view_interface` and
`container_interface`! If you don't ever write iterators, range views, or
containers, this is not for you.
Online docs: https://boostorg.github.io/stl_interfaces.
This library includes a temporary implementation for those who wish to experiment with
a concept-constrained version before C++20 is widely implemented. Casey Carter's cmcstl2
is an implementation of the `std::ranges` portion of the C++20 standard library. To use it:
- check out the cmcstl2 branch of this library; then
- put its headers in your include path, so that they can be included with
`#include <stl2/foo.hpp>`; and
- build with GCC 8 or 9, including the `-fconcepts` and `-std=c++2a` flags.
GCC 8 and 9 are the only compilers with an adequate concepts implementation at
the time of this writing.
[![Build Status](https://travis-ci.org/boostorg/stl_interfaces.svg?branch=develop)](https://travis-ci.org/boostorg/stl_interfaces)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/tzlaine/stl-interfaces?branch=develop&svg=true)](https://ci.appveyor.com/project/tzlaine/stl-interfaces)
[![License](https://img.shields.io/badge/license-boost-brightgreen.svg)](LICENSE_1_0.txt)

View File

@@ -0,0 +1,38 @@
# Copyright Louis Dionne 2016
# Copyright Zach Laine 2016-2017
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
###############################################################################
# Boost
###############################################################################
set(Boost_USE_STATIC_LIBS ON)
if (NOT BOOST_BRANCH)
set(BOOST_BRANCH master)
endif()
add_custom_target(
boost_root_clone
git clone --depth 100 -b ${BOOST_BRANCH}
https://github.com/boostorg/boost.git boost_root
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
if (MSVC)
set(bootstrap_cmd ./bootstrap.bat)
else()
set(bootstrap_cmd ./bootstrap.sh)
endif()
add_custom_target(
boost_clone
COMMAND git submodule init libs/assert
COMMAND git submodule init libs/config
COMMAND git submodule init libs/core
COMMAND git submodule init tools/build
COMMAND git submodule init libs/headers
COMMAND git submodule init tools/boost_install
COMMAND git submodule update --jobs 3
COMMAND ${bootstrap_cmd}
COMMAND ./b2 headers
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/boost_root
DEPENDS boost_root_clone)
add_library(boost INTERFACE)
add_dependencies(boost boost_clone)
target_include_directories(boost INTERFACE ${CMAKE_BINARY_DIR}/boost_root)

View File

@@ -0,0 +1,14 @@
# Copyright (C) 2019 T. Zachary Laine
#
# 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)
include_directories(${CMAKE_HOME_DIRECTORY})
macro(add_snippets name)
add_executable(${name} ${name}.cpp)
target_link_libraries(${name} yap)
if (clang_on_linux)
target_link_libraries(${name} c++)
endif ()
endmacro()

View File

@@ -0,0 +1,113 @@
# Distributed under the Boost Software License, Version 1.0. (See
# accomanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt
import path ;
import doxygen ;
import quickbook ;
# using auto-index ;
using doxygen ;
using quickbook ;
using boostbook ;
path-constant here : . ;
rule run_doxygen ( files * : name : expand ? )
{
expand ?= <doxygen:param>EXPAND_ONLY_PREDEF=YES ;
doxygen stl_interfaces_reference
:
$(files)
:
<doxygen:param>EXTRACT_ALL=YES
# note that there is no detail::unspecified -- this is a hack to get all
# the SFINAE code out of the API docs.
<doxygen:param>"PREDEFINED=\"BOOST_STL_INTERFACES_DOXYGEN=1\" \\
\"enable_if=detail::unspecified\""
<doxygen:param>HIDE_UNDOC_MEMBERS=NO
<doxygen:param>EXTRACT_PRIVATE=NO
<doxygen:param>ENABLE_PREPROCESSING=YES
<doxygen:param>MACRO_EXPANSION=YES
$(expand)
<doxygen:param>SEARCH_INCLUDES=NO
<doxygen:param>EXAMPLE_PATH=.
<reftitle>$(name)
;
}
run_doxygen [ glob $(here)/../../../boost/stl_interfaces/*.hpp ] : "Headers" ;
install images_standalone : [ glob *.png ] : <location>html/stl_interfaces/img ;
explicit images_standalone ;
install images_boostdoc : [ glob *.png ] : <location>../../../doc/html/stl_interfaces/img ;
explicit images_boostdoc ;
xml stl_interfaces
:
stl_interfaces.qbk
:
<dependency>stl_interfaces_reference
;
boostbook standalone
:
stl_interfaces
:
# HTML options first:
# Use graphics not text for navigation:
<xsl:param>navig.graphics=1
# How far down we chunk nested sections, basically all of them:
<xsl:param>chunk.section.depth=10
# Don't put the first section on the same page as the TOC:
<xsl:param>chunk.first.sections=1
# How far down sections get TOC's
<xsl:param>toc.section.depth=10
# Max depth in each TOC:
<xsl:param>toc.max.depth=4
# How far down we go with TOC's
<xsl:param>generate.section.toc.level=10
# Set the path to the boost-root so we find our graphics:
#<xsl:param>boost.root="../../../.."
# location of the main index file so our links work:
#<xsl:param>boost.libraries=../../../../../libs/libraries.htm
# PDF Options:
# TOC Generation: this is needed for FOP-0.9 and later:
# <xsl:param>fop1.extensions=1
<xsl:param>xep.extensions=1
# TOC generation: this is needed for FOP 0.2, but must not be set to zero for FOP-0.9!
<xsl:param>fop.extensions=0
# No indent on body text:
<xsl:param>body.start.indent=0pt
# Margin size:
<xsl:param>page.margin.inner=0.5in
# Margin size:
<xsl:param>page.margin.outer=0.5in
# Yes, we want graphics for admonishments:
<xsl:param>admon.graphics=1
# Set this one for PDF generation *only*:
# default pnd graphics are awful in PDF form,
# better use SVG's instead:
# <xsl:param>admon.graphics.extension=".svg"
# <auto-index>on
# <auto-index-verbose>on
# <auto-index-internal>on
# <auto-index-script>stl_interfaces.idx
# <quickbook-define>enable_index
# <auto-index-prefix>..
# <xsl:param>index.on.type=1
<dependency>images_standalone
;
alias boostdoc : stl_interfaces : : : <dependency>images_boostdoc ;
explicit boostdoc ;
alias boostrelease ;
explicit boostrelease ;

View File

@@ -0,0 +1,38 @@
[/
/ 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)
/]
[section Examples]
[section Random Access Iterator]
[random_access_iterator]
[endsect]
[section Mutable and Constant Iterator Interoperability]
[interoperability]
[endsect]
[section Zip Iterator / Proxy Iterator]
[zip_proxy_iterator]
[endsect]
[section Reimplementing `back_insert_iterator`]
[back_insert_iterator]
[endsect]
[section Reimplementing `reverse_iterator`]
[reverse_iterator]
[endsect]
[endsect]

View File

@@ -0,0 +1,219 @@
[/
/ 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)
/]
[section Introduction]
Writing STL iterators, views, and containers is surprisingly hard. There are a
lot of things that can subtly go wrong. It is also very tedious, which of
course makes it error-prone.
Iterators have numerous typedefs and operations, even though all the
operations of a given iterator can be implemented in terms of at most four
operations (and usually only three). Writing all the other operations yields
very similar-looking code that is hard to review, and all but requires that
you write full-coverage tests for each iterator.
Writing view types like those found in `std::ranges` is also laborious,
considering that most of each view type's API can be derived from `begin()`
and `end()`. C++20 has a template that does exactly this,
`std::ranges::view_interface`; _IFaces_ provides a pre-C++20-friendly
implementation.
Most daunting of all is the task of writing a type or template that meets the
container requirements in the standard. _IFaces_ provides another template
called _cont_iface_ that reduces the implementation and testing burden
dramatically.
[note C++20 versions of _iter_iface_ and _cont_iface_ are provided (C++20
provides `std::view_interface`). These are constrained templates using C++20
concepts. These are in the `boost::stl_interfaces::v2` namespace, and are
considered experimental, because at the time of this writing, no
C++20-conforming compiler exists.]
[heading A Quick Example]
Here is an example of the iterator portion of the library. Let's say that we
wanted to make a random access iterator that represents a string of arbitrary
length constructed by repeating a shorter string. Let's call this iterator
`repeated_chars_iterator`. Here it is in action:
[repeated_chars_iterator_usage]
There's nothing in the standard library that gets us that kind of behavior, so
we have to write it. This library seeks to turn what we write from this:
struct repeated_chars_iterator
{
using value_type = char;
using difference_type = std::ptrdiff_t;
using pointer = char const *;
using reference = char const;
using iterator_category = std::random_access_iterator_tag;
constexpr repeated_chars_iterator() noexcept :
first_(nullptr),
size_(0),
n_(0)
{}
constexpr repeated_chars_iterator(
char const * first,
difference_type size,
difference_type n) noexcept :
first_(first),
size_(size),
n_(n)
{}
constexpr reference operator*() const noexcept
{
return first_[n_ % size_];
}
constexpr value_type operator[](difference_type n) const noexcept
{
return first_[(n_ + n) % size_];
}
constexpr repeated_chars_iterator & operator++() noexcept
{
++n_;
return *this;
}
constexpr repeated_chars_iterator operator++(int)noexcept
{
repeated_chars_iterator retval = *this;
++*this;
return retval;
}
constexpr repeated_chars_iterator & operator+=(difference_type n) noexcept
{
n_ += n;
return *this;
}
constexpr repeated_chars_iterator & operator--() noexcept
{
--n_;
return *this;
}
constexpr repeated_chars_iterator operator--(int)noexcept
{
repeated_chars_iterator retval = *this;
--*this;
return retval;
}
constexpr repeated_chars_iterator & operator-=(difference_type n) noexcept
{
n_ -= n;
return *this;
}
friend constexpr bool operator==(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs.first_ == rhs.first_ && lhs.n_ == rhs.n_;
}
friend constexpr bool operator!=(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return !(lhs == rhs);
}
friend constexpr bool operator<(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs.first_ == rhs.first_ && lhs.n_ < rhs.n_;
}
friend constexpr bool operator<=(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs == rhs || lhs < rhs;
}
friend constexpr bool operator>(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return rhs < lhs;
}
friend constexpr bool operator>=(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return rhs <= lhs;
}
friend constexpr repeated_chars_iterator
operator+(repeated_chars_iterator lhs, difference_type rhs) noexcept
{
return lhs += rhs;
}
friend constexpr repeated_chars_iterator
operator+(difference_type lhs, repeated_chars_iterator rhs) noexcept
{
return rhs += lhs;
}
friend constexpr repeated_chars_iterator
operator-(repeated_chars_iterator lhs, difference_type rhs) noexcept
{
return lhs -= rhs;
}
friend constexpr difference_type operator-(
repeated_chars_iterator lhs, repeated_chars_iterator rhs) noexcept
{
return lhs.n_ - rhs.n_;
}
private:
char const * first_;
difference_type size_;
difference_type n_;
};
(that's a lot of code!) into this:
[repeated_chars_iterator]
Ah, that's better. Both of these definitions for `repeated_chars_iterator`
have the same semantics and performance profile. It's just a lot less code to
write the second one, and writing the second one is more novice-friendly.
[note _IFaces_'s `iterator_interface` implements iterators that model the
C++20 iterator concepts.]
[endsect]
[section This Library's Relationship to Boost.Iterator]
_Iterator_ is a library that is already in Boost, and it has been around for a
long time.
However, it was attempting to solve a lot of problems related to iterators,
not just how to write them from scratch. It is also not easy to modernize it
for use in C++11 and later. Specifically:
- _Iterator_ contains a large number of iterator adaptors; those have since
been rendered moot by C++20 ranges.
- _Iterator_'s `iterator_facade` template is not limited just to the
existing standard C++ iterator categories; that was an experiment that never
landed in standard C++, so it adds needless complexity.
- _Iterator_'s `iterator_facade` was written against C++98, so it is not
`constexpr`- and `noexcept`-friendly.
- _Iterator_'s `iterator_facade` does not support proxy iterators, which
are fully supported by the C++20 iterator concepts.
- There is opportunity to reduce the amount of code the user must write in
order to use `iterator_facade`.
- _Iterator_ contains two templates, `iterator_facade` and `iterator_adaptor`,
that represent two ways of writing a new iterator while writing as little
code as possible. It would be nice to have the functionality for both
available in one template, but it is difficult to unify those two templates
as written.
For these reasons, it seems more appropriate to introduce a new Boost library
than to try and address the shortcomings of _Iterator_'s `iterator_facade` and
`iterator_adaptor` templates directly.
[endsect]

View File

@@ -0,0 +1,96 @@
[/
/ 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)
/]
[section Rationale]
[heading There Are Minimal Derived-Type Contraints]
This is the constraint on the `Derived` template parameter to _iter_iface_,
_view_iface_ and _cont_iface_:
std::enable_if_t<
std::is_class<Derived>::value &&
std::is_same<Derived, std::remove_cv_t<Derived>>::value>
This prevents instantiating an interface template with an `int`, a `const`
type, a reference type, etc.
Further constraints are not possible (for instance, that _view_iface_ is given
a `Derived` template parameter for a type that has a `begin()` and `end()`),
because `Derived` is an incomplete type within each *`_interface` template.
[heading Using a Special Access-Granting `struct`]
The interface templates rely mostly on public members provided by their
`Derived` template parameter. However, _iter_iface_ requires you to supply
`base_reference()` functions if you want it to act like an adaptor. Since at
least the non-`const` overload provides a non-`const` lvalue reference to one
of your types data members, it will break the encapsulation of many types to
leave `base_reference()` a public member. To allow users to keep these
overloads private, _access_ exists.
[heading _iter_iface_ Can Act Like an Adaptor, And the Other Interface Templates Can't]
There wouldn't be much point in adding this functionality to _view_iface_,
because it only uses the `begin()` and `end()` of the `Derived` type anyway.
For _cont_iface_ it also does not make much sense. Consider how many
container adaptors you've written. That's a use case that does not come up
often.
[heading _iter_iface_ Takes a Lot of Template Parameters, And the Other Interface Templates Don't]
_iter_iface_ does in fact take a lot of template parameters. However, it
usually only takes three: the `Derived` type, the iterator category, and the
iterator's `value_type`.
When you make a proxy iterator, you typically use the _proxy_iter_iface_
alias, and you again only need the same three template parameters. Though you
can opt into more template parameters, the rest are seldom necessary.
By contrast, the _view_iface_ and _cont_iface_ templates have very few
template parameters. For _view_iface_, this is because there are no member
typedefs in the `view` concept. For _cont_iface_, it was deemed ridiculous to
create a template whose purpose is to reduce code size, which takes 14
template parameters.
[heading _cont_iface_ Does not Deduce Nested Types Like `iterator`]
_cont_iface_ could deduce some of the nested types required for a standard
sequence container. For instance, `iterator` can be deduced as
`decltype(*begin())`. However, a type `D` derived from _cont_iface_ may need
to use some of these nested types _emdash_ like `iterator` _emdash_ in its
interface or implementation. If this is the case, those nested types are not
available early enough in the parse to be used in `D`, if they come from
deductions in _cont_iface_. This leaves the user in the awkward position of
defining the same nested type with a different name that can be used within
`D`. It seems better to leave these types for the user to define.
[heading _cont_iface_ Does not Support Associative or Unordered Associative Containers]
That's right. Associative containers have an interface that assumes that they
are node-based containers. On modern hardware, node-based containers are not
very efficient, and I don't want to encourage people to write more of them.
Unordered associative containers have an interface that precludes open
addressing. I don't want to encourage more of that either.
[heading _cont_iface_ Does not Satisfy the Allocator-Aware Container Requirements]
It may not be immediately obvious, but _cont_iface_ simply cannot help with
the allocator-aware requirements. All of the allocator-aware requirements but
3 are special members and constructors. A _CRTP_ base template is unable to
provide those, based on the language rules. That leaves the `allocator_type`
typedef, which the user must provide; member `swap()`, which is already a
container requirement (the allocator-aware table entry just specifies that
member `swap()` must be constant-time); and `get_allocator()`, which again is
something the user must provide.
Most of the difficulty of dealing with allocators has to do with the
implementation details of their use within your container. _cont_iface_
provides missing elements of a sequence container's interface, by calling
user-provided members of that same interface. It cannot help you with your
container's implementation.
[endsect]

View File

@@ -0,0 +1,3 @@
!scan-path "boost/stl_interfaces" ".*\.hpp" true
!scan-path "libs/stl_interfaces/example" ".*\.cpp"

View File

@@ -0,0 +1,89 @@
[/
/ 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)
/]
[library Boost.STLInterfaces
[quickbook 1.3]
[authors [Laine, Zach]]
[copyright 2019 T. Zachary Laine]
[category template]
[id stl_interfaces]
[dirname stl_interfaces]
[purpose
A string and rope library targeting standardization.
]
[license
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])
]
]
[/ QuickBook Document version 1.3 ]
[/ Imports ]
[/ Iterator Examples ]
[import ../example/repeated_chars_iterator.cpp]
[import ../example/node_iterator.cpp]
[import ../example/filtered_int_iterator.cpp]
[import ../example/random_access_iterator.cpp]
[import ../example/interoperability.cpp]
[import ../example/zip_proxy_iterator.cpp]
[import ../example/back_insert_iterator.cpp]
[import ../example/reverse_iterator.cpp]
[/ View Examples ]
[import ../example/drop_while_view.cpp]
[/ Container Examples ]
[import ../example/static_vector.hpp]
[import ../example/static_vector.cpp]
[/ Images ]
[def __note__ [$images/note.png]]
[def __tip__ [$images/tip.png]]
[def __important__ [$images/important.png]]
[def __caution__ [$images/caution.png]]
[def __warning__ [$images/warning.png]]
[/ Links ]
[def _IFaces_ Boost.STLInterfaces]
[def _iter_iface_ [classref boost::stl_interfaces::v1::iterator_interface `iterator_interface`]]
[def _proxy_iter_iface_ [classref boost::stl_interfaces::v1::proxy_iterator_interface `proxy_iterator_interface`]]
[def _view_iface_ [classref boost::stl_interfaces::v1::view_interface `view_interface`]]
[def _cont_iface_ [classref boost::stl_interfaces::v1::sequence_container_interface `sequence_container_interface`]]
[def _rev_iter_ [classref boost::stl_interfaces::v1::reverse_iterator `reverse_iterator`]]
[def _access_ [classref boost::stl_interfaces::access `access`]]
[def _concept_m_ [macroref BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT]]
[def _traits_m_ [macroref BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS]]
[def _CRTP_ [@https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern CRTP]]
[def _Iterator_ [@https://www.boost.org/doc/libs/release/libs/iterator Boost.Iterator]]
[def _Container_ [@https://www.boost.org/doc/libs/release/libs/container Boost.Container]]
[def _emdash_ \u2014]
[include intro.qbk]
[include tutorial.qbk]
[include examples.qbk]
[section Compiler Support]
_IFaces_ is written against the C++14 standard. It is targetting
standardization, and the changes required to make it C++11-compatible were
considered too great.
_IFaces_ should work with any conforming C++14 compiler. It has been tested with Clang, GCC, and Visual Studio.
[endsect]
[section Reference]
[xinclude stl_interfaces_reference.xml]
[endsect]
[include rationale.qbk]

View File

@@ -0,0 +1,591 @@
<?xml version="1.0" standalone="yes"?>
<library-reference id="headers"><title>Headers</title><header name="boost/stl_interfaces/fwd.hpp">
<namespace name="boost">
<namespace name="stl_interfaces">
<namespace name="v1">
<namespace name="v1_dtl">
<struct name="decrementable_sentinel"><template>
<template-type-parameter name="Range"/>
<template-type-parameter name=""><default>void</default></template-type-parameter>
</template><inherit access="public">false_type</inherit></struct><struct-specialization name="decrementable_sentinel"><template>
<template-type-parameter name="Range"/>
</template><specialization><template-arg>Range</template-arg><template-arg>void_t&lt; decltype(--std::declval&lt; sentinel_t&lt; Range &gt; &amp; &gt;())&gt;</template-arg></specialization><inherit access="public">true_type</inherit></struct-specialization><struct name="iterator"><template>
<template-type-parameter name="Range"/>
<template-type-parameter name=""><default>void</default></template-type-parameter>
</template></struct><struct-specialization name="iterator"><template>
<template-type-parameter name="Range"/>
</template><specialization><template-arg>Range</template-arg><template-arg>void_t&lt; decltype(std::declval&lt; Range &amp; &gt;().begin())&gt;</template-arg></specialization><typedef name="type"><type>decltype(std::declval&lt; Range &amp; &gt;().begin())</type></typedef>
</struct-specialization><struct name="sentinel"><template>
<template-type-parameter name="Range"/>
<template-type-parameter name=""><default>void</default></template-type-parameter>
</template></struct><struct-specialization name="sentinel"><template>
<template-type-parameter name="Range"/>
</template><specialization><template-arg>Range</template-arg><template-arg>void_t&lt; decltype(std::declval&lt; Range &amp; &gt;().end())&gt;</template-arg></specialization><typedef name="type"><type>decltype(std::declval&lt; Range &amp; &gt;().end())</type></typedef>
</struct-specialization><typedef name="void_t"><type>void</type></typedef>
<typedef name="iter_difference_t"><type>typename std::iterator_traits&lt; Iter &gt;::difference_type</type></typedef>
<typedef name="iterator_t"><type>typename <classname>iterator</classname>&lt; Range &gt;::type</type></typedef>
<typedef name="sentinel_t"><type>typename <classname>sentinel</classname>&lt; Range &gt;::type</type></typedef>
<typedef name="range_difference_t"><type>iter_difference_t&lt; iterator_t&lt; Range &gt; &gt;</type></typedef>
<typedef name="common_range"><type>std::is_same&lt; iterator_t&lt; Range &gt;, sentinel_t&lt; Range &gt; &gt;</type></typedef>
</namespace>
<enum name="element_layout"><enumvalue name="discontiguous"><default>= false</default></enumvalue><enumvalue name="contiguous"><default>= true</default></enumvalue><description><para>An enumeration used to indicate whether the underlying data have a contiguous or discontiguous layout when instantiating <computeroutput><classname alt="boost::stl_interfaces::v1::view_interface">view_interface</classname></computeroutput> and <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></enum>
</namespace>
</namespace>
</namespace>
</header>
<header name="boost/stl_interfaces/iterator_interface.hpp">
<namespace name="boost">
<namespace name="stl_interfaces">
<struct name="access"><description><para>A type for granting access to the private members of an iterator derived from <computeroutput>iterator_interface</computeroutput>. </para></description></struct><struct name="proxy_arrow_result"><template>
<template-type-parameter name="T"/>
</template><description><para>The return type of <computeroutput>operator-&gt;()</computeroutput> in a proxy iterator.</para><para>This template is used as the default <computeroutput>Pointer</computeroutput> template parameter in the <computeroutput>proxy_iterator_interface</computeroutput> template alias. Note that the use of this template implies a copy or move of the underlying object of type <computeroutput>T</computeroutput>. </para></description><method-group name="public member functions">
<method name="operator-&gt;" cv="const noexcept"><type>constexpr T const *</type></method>
<method name="operator-&gt;" cv="noexcept"><type>constexpr T *</type></method>
</method-group>
<constructor cv="noexcept(noexcept(T(value))))"><parameter name="value"><paramtype>T const &amp;</paramtype></parameter></constructor>
<constructor cv="noexcept(noexcept(T(std::move(value)))))"><parameter name="value"><paramtype>T &amp;&amp;</paramtype></parameter></constructor>
</struct><namespace name="v1">
<struct name="iterator_interface"><template>
<template-type-parameter name="Derived"/>
<template-type-parameter name="IteratorConcept"/>
<template-type-parameter name="ValueType"/>
<template-type-parameter name="Reference"/>
<template-type-parameter name="Pointer"/>
<template-type-parameter name="DifferenceType"/>
</template><description><para>A CRTP template that one may derive from to make defining iterators easier.</para><para>The template parameter <computeroutput>D</computeroutput> for <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> may be an incomplete type. Before any member of the resulting specialization of <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> other than special member functions is referenced, <computeroutput>D</computeroutput> shall be complete, and model <computeroutput>std::derived_from&lt;<classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname>&lt;D&gt;&gt;</computeroutput>. </para></description><typedef name="iterator_concept"><type>IteratorConcept</type></typedef>
<typedef name="iterator_category"><type><emphasis>unspecified</emphasis></type></typedef>
<typedef name="value_type"><type>std::remove_const_t&lt; ValueType &gt;</type></typedef>
<typedef name="reference"><type>Reference</type></typedef>
<typedef name="pointer"><type><emphasis>unspecified</emphasis></type></typedef>
<typedef name="difference_type"><type>DifferenceType</type></typedef>
<method-group name="public member functions">
<method name="operator *" cv="const noexcept(noexcept(*access::base(std::declval&lt; D const &amp; &gt;()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="operator-&gt;" cv="const noexcept(noexcept(detail::make_pointer&lt; pointer &gt;(*std::declval&lt; D const &amp; &gt;()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="operator[]" cv="const noexcept(noexcept(D(std::declval&lt; D const &amp; &gt;()), std::declval&lt; D &amp; &gt;()+=i, *std::declval&lt; D &amp; &gt;())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="i"><paramtype>difference_type</paramtype></parameter></method>
<method name="operator++" cv="noexcept(noexcept(++access::base(std::declval&lt; D &amp; &gt;()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;!<classname alt="boost::stl_interfaces::v1::v1_dtl::plus_eq">v1_dtl::plus_eq</classname>&lt;D, difference_type&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="operator++" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;()+=difference_type(1))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="operator++" cv="noexcept(noexcept(D(std::declval&lt; D &amp; &gt;()),++std::declval&lt; D &amp; &gt;())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name=""><paramtype>int</paramtype></parameter></method>
<method name="operator+=" cv="noexcept(noexcept(access::base(std::declval&lt; D &amp; &gt;())+=n)))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="n"><paramtype>difference_type</paramtype></parameter></method>
<method name="operator+" cv="const noexcept(noexcept(D(std::declval&lt; D &amp; &gt;()), std::declval&lt; D &amp; &gt;()+=i)))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="i"><paramtype>difference_type</paramtype></parameter></method>
<method name="operator--" cv="noexcept(noexcept(--access::base(std::declval&lt; D &amp; &gt;()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;!<classname alt="boost::stl_interfaces::v1::v1_dtl::plus_eq">v1_dtl::plus_eq</classname>&lt;D, difference_type&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="operator--" cv="noexcept(noexcept(D(std::declval&lt; D &amp; &gt;()), std::declval&lt; D &amp; &gt;()+=-difference_type(1))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="operator--" cv="noexcept(noexcept(D(std::declval&lt; D &amp; &gt;()), --std::declval&lt; D &amp; &gt;())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name=""><paramtype>int</paramtype></parameter></method>
<method name="operator-=" cv="noexcept"><type>constexpr D &amp;</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="i"><paramtype>difference_type</paramtype></parameter></method>
<method name="operator-" cv="const noexcept(noexcept(access::base(std::declval&lt; D const &amp; &gt;()) - access::base(other))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="other"><paramtype>D</paramtype></parameter></method>
</method-group>
</struct><namespace name="v1_dtl">
<struct name="plus_eq"><template>
<template-type-parameter name="Iterator"/>
<template-type-parameter name="DifferenceType"/>
<template-type-parameter name=""><default>void</default></template-type-parameter>
</template><inherit access="public">false_type</inherit></struct><struct-specialization name="plus_eq"><template>
<template-type-parameter name="Iterator"/>
<template-type-parameter name="DifferenceType"/>
</template><specialization><template-arg>Iterator</template-arg><template-arg>DifferenceType</template-arg><template-arg>void_t&lt; decltype(std::declval&lt; Iterator &amp; &gt;()+=std::declval&lt; DifferenceType &gt;())&gt;</template-arg></specialization><inherit access="public">true_type</inherit></struct-specialization><struct name="ra_iter"><template>
<template-type-parameter name="Iterator"/>
<template-type-parameter name=""><default>void</default></template-type-parameter>
</template><inherit access="public">false_type</inherit></struct><struct-specialization name="ra_iter"><template>
<template-type-parameter name="Iterator"/>
</template><specialization><template-arg>Iterator</template-arg><template-arg>void_t&lt; typename Iterator::iterator_concept &gt;</template-arg></specialization><inherit access="public">std::integral_constant&lt; bool, std::is_base_of&lt; std::random_access_iterator_tag, Iterator::iterator_concept &gt;::value &gt;</inherit></struct-specialization>
<function name="derived_iterator"><type>void</type><template>
<template-type-parameter name="D"/>
<template-type-parameter name="IteratorConcept"/>
<template-type-parameter name="ValueType"/>
<template-type-parameter name="Reference"/>
<template-type-parameter name="Pointer"/>
<template-type-parameter name="DifferenceType"/>
</template><parameter name=""><paramtype><classname>iterator_interface</classname>&lt; D, IteratorConcept, ValueType, Reference, Pointer, DifferenceType &gt; const &amp;</paramtype></parameter></function>
</namespace>
<typedef name="proxy_iterator_interface"><description><para>A template alias useful for defining proxy iterators. <para><emphasis role="bold">See Also:</emphasis><para><computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput>. </para>
</para>
</para></description><type><classname>iterator_interface</classname>&lt; Derived, IteratorConcept, ValueType, Reference, <classname>proxy_arrow_result</classname>&lt; Reference &gt;, DifferenceType &gt;</type></typedef>
<function name="operator=="><type>constexpr auto</type><template>
<template-type-parameter name="IteratorInterface1"/>
<template-type-parameter name="IteratorInterface2"/>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;!v1_dtl::ra_iter&lt;IteratorInterface1&gt;::value&gt;</default></template-type-parameter>
</template><parameter name="lhs"><paramtype>IteratorInterface1</paramtype></parameter><parameter name="rhs"><paramtype>IteratorInterface2</paramtype></parameter><description><para>Implementation of <computeroutput>operator==()</computeroutput>, implemented in terms of the iterator underlying IteratorInterface, for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput>, except those with an iterator category derived from <computeroutput>std::random_access_iterator_tag</computeroutput>.</para><para>Implementation of <computeroutput>operator==()</computeroutput> for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> that have an iterator category derived from <computeroutput>std::random_access_iterator_tag</computeroutput>. </para></description></function>
<function name="operator!="><type>constexpr auto</type><template>
<template-type-parameter name="IteratorInterface1"/>
<template-type-parameter name="IteratorInterface2"/>
</template><parameter name="lhs"><paramtype>IteratorInterface1</paramtype></parameter><parameter name="rhs"><paramtype>IteratorInterface2</paramtype></parameter><description><para>Implementation of <computeroutput>operator!=()</computeroutput> for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput>. </para></description></function>
<function name="operator&lt;"><type>constexpr auto</type><template>
<template-type-parameter name="IteratorInterface1"/>
<template-type-parameter name="IteratorInterface2"/>
</template><parameter name="lhs"><paramtype>IteratorInterface1</paramtype></parameter><parameter name="rhs"><paramtype>IteratorInterface2</paramtype></parameter><description><para>Implementation of <computeroutput>operator&lt;()</computeroutput> for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> that have an iterator category derived from <computeroutput>std::random_access_iterator_tag</computeroutput>. </para></description></function>
<function name="operator&lt;="><type>constexpr auto</type><template>
<template-type-parameter name="IteratorInterface1"/>
<template-type-parameter name="IteratorInterface2"/>
</template><parameter name="lhs"><paramtype>IteratorInterface1</paramtype></parameter><parameter name="rhs"><paramtype>IteratorInterface2</paramtype></parameter><description><para>Implementation of <computeroutput>operator&lt;=()</computeroutput> for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> that have an iterator category derived from <computeroutput>std::random_access_iterator_tag</computeroutput>. </para></description></function>
<function name="operator&gt;"><type>constexpr auto</type><template>
<template-type-parameter name="IteratorInterface1"/>
<template-type-parameter name="IteratorInterface2"/>
</template><parameter name="lhs"><paramtype>IteratorInterface1</paramtype></parameter><parameter name="rhs"><paramtype>IteratorInterface2</paramtype></parameter><description><para>Implementation of <computeroutput>operator&gt;()</computeroutput> for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> that have an iterator category derived from <computeroutput>std::random_access_iterator_tag</computeroutput>. </para></description></function>
<function name="operator&gt;="><type>constexpr auto</type><template>
<template-type-parameter name="IteratorInterface1"/>
<template-type-parameter name="IteratorInterface2"/>
</template><parameter name="lhs"><paramtype>IteratorInterface1</paramtype></parameter><parameter name="rhs"><paramtype>IteratorInterface2</paramtype></parameter><description><para>Implementation of <computeroutput>operator&gt;=()</computeroutput> for all iterators derived from <computeroutput><classname alt="boost::stl_interfaces::v1::iterator_interface">iterator_interface</classname></computeroutput> that have an iterator category derived from <computeroutput>std::random_access_iterator_tag</computeroutput>. </para></description></function>
</namespace>
</namespace>
</namespace>
<macro name="BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT" kind="functionlike"><macro-parameter name="type"/><macro-parameter name="concept_name"/><description><para><computeroutput>static_asserts</computeroutput> that type <computeroutput>type</computeroutput> models concept <computeroutput>concept_name</computeroutput>. This is useful for checking that an iterator, view, etc. that you write using one of the *<computeroutput>_interface</computeroutput> templates models the right C++ concept.</para><para>For example: <computeroutput>BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(my_iter, std::input_iterator)</computeroutput>.</para><para><note><para>This macro expands to nothing when <computeroutput>__cpp_lib_concepts</computeroutput> is not defined. </para>
</note>
</para></description></macro>
<macro name="BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS" kind="functionlike"><macro-parameter name="iter"/><macro-parameter name="category"/><macro-parameter name="concept"/><macro-parameter name="value_type"/><macro-parameter name="reference"/><macro-parameter name="pointer"/><macro-parameter name="difference_type"/><description><para><computeroutput>static_asserts</computeroutput> that the types of all typedefs in <computeroutput>std::iterator_traits&lt;iter&gt;</computeroutput> match the remaining macro parameters. This is useful for checking that an iterator you write using <computeroutput>iterator_interface</computeroutput> has the correct iterator traits.</para><para>For example: <computeroutput>BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(my_iter, std::input_iterator_tag, std::input_iterator_tag, int, int &amp;, int *, std::ptrdiff_t)</computeroutput>.</para><para><note><para>This macro ignores the <computeroutput>concept</computeroutput> parameter when <computeroutput>__cpp_lib_concepts</computeroutput> is not defined. </para>
</note>
</para></description></macro>
</header>
<header name="boost/stl_interfaces/reverse_iterator.hpp">
<namespace name="boost">
<namespace name="stl_interfaces">
<namespace name="v1">
<struct name="reverse_iterator"><template>
<template-type-parameter name="BidiIter"/>
</template><inherit access="public">boost::stl_interfaces::v1::iterator_interface&lt; reverse_iterator&lt; BidiIter &gt;, std::iterator_traits&lt; BidiIter &gt;::iterator_category, std::iterator_traits&lt; BidiIter &gt;::value_type, std::iterator_traits&lt; BidiIter &gt;::reference, std::iterator_traits&lt; BidiIter &gt;::pointer, std::iterator_traits&lt; BidiIter &gt;::difference_type &gt;</inherit><description><para>This type is very similar to the C++20 version of <computeroutput>std::reverse_iterator</computeroutput>; it is <computeroutput>constexpr</computeroutput>-, <computeroutput>noexcept</computeroutput>-, and proxy-friendly. </para></description><method-group name="friend functions">
<method name="reverse_iterator"><type>friend struct</type><template>
<template-type-parameter name="BidiIter2"/>
</template><description><para>A template alias for <computeroutput>std::reverse_iterator</computeroutput>. This only exists to make migration from Boost.STLInterfaces to C++20 easier; switch to the one in <computeroutput>std</computeroutput> as soon as you can. </para></description></method>
</method-group>
<method-group name="public member functions">
<method name="operator *" cv="const noexcept(noexcept(std::prev(v1_dtl::ce_prev(std::declval&lt; BidiIter &amp; &gt;())))))"><type>constexpr std::iterator_traits&lt; BidiIter &gt;::reference</type></method>
<method name="operator+=" cv="noexcept(noexcept(v1_dtl::ce_adv(std::declval&lt; BidiIter &amp; &gt;(), -n, typename std::iterator_traits&lt; BidiIter &gt;::iterator_category{}))))"><type>constexpr <classname>reverse_iterator</classname> &amp;</type><parameter name="n"><paramtype>typename std::iterator_traits&lt; BidiIter &gt;::difference_type</paramtype></parameter></method>
<method name="base" cv="const noexcept"><type>constexpr BidiIter</type></method>
</method-group>
<constructor cv="noexcept(noexcept(BidiIter())))"/>
<constructor cv="noexcept(noexcept(BidiIter(it))))"><parameter name="it"><paramtype>BidiIter</paramtype></parameter></constructor>
<constructor><template>
<template-type-parameter name="BidiIter2"/>
<template-type-parameter name="E"><default>std::enable_if_t&lt; std::is_convertible&lt;BidiIter2, BidiIter&gt;::value&gt;</default></template-type-parameter>
</template><parameter name="it"><paramtype><classname>reverse_iterator</classname>&lt; BidiIter2 &gt; const &amp;</paramtype></parameter></constructor>
<method-group name="private member functions">
<method name="base_reference" cv="noexcept"><type>constexpr BidiIter &amp;</type></method>
<method name="base_reference" cv="const noexcept"><type>constexpr BidiIter const &amp;</type></method>
</method-group>
</struct><namespace name="v1_dtl">
<function name="ce_dist"><type>constexpr auto</type><template>
<template-type-parameter name="Iter"/>
</template><parameter name="f"><paramtype>Iter</paramtype></parameter><parameter name="l"><paramtype>Iter</paramtype></parameter><parameter name=""><paramtype>std::random_access_iterator_tag</paramtype></parameter></function>
<function name="ce_dist"><type>constexpr auto</type><template>
<template-type-parameter name="Iter"/>
<template-type-parameter name="Tag"/>
</template><parameter name="f"><paramtype>Iter</paramtype></parameter><parameter name="l"><paramtype>Iter</paramtype></parameter><parameter name=""><paramtype>Tag</paramtype></parameter></function>
<function name="ce_prev"><type>constexpr Iter</type><template>
<template-type-parameter name="Iter"/>
</template><parameter name="it"><paramtype>Iter</paramtype></parameter></function>
<function name="ce_adv"><type>constexpr void</type><template>
<template-type-parameter name="Iter"/>
<template-type-parameter name="Offset"/>
</template><parameter name="f"><paramtype>Iter &amp;</paramtype></parameter><parameter name="n"><paramtype>Offset</paramtype></parameter><parameter name=""><paramtype>std::random_access_iterator_tag</paramtype></parameter></function>
<function name="ce_adv"><type>constexpr void</type><template>
<template-type-parameter name="Iter"/>
<template-type-parameter name="Offset"/>
<template-type-parameter name="Tag"/>
</template><parameter name="f"><paramtype>Iter &amp;</paramtype></parameter><parameter name="n"><paramtype>Offset</paramtype></parameter><parameter name=""><paramtype>Tag</paramtype></parameter></function>
</namespace>
<function name="operator=="><type>constexpr auto</type><template>
<template-type-parameter name="BidiIter"/>
</template><parameter name="lhs"><paramtype><classname>reverse_iterator</classname>&lt; BidiIter &gt;</paramtype></parameter><parameter name="rhs"><paramtype><classname>reverse_iterator</classname>&lt; BidiIter &gt;</paramtype></parameter></function>
<function name="operator=="><type>constexpr auto</type><template>
<template-type-parameter name="BidiIter1"/>
<template-type-parameter name="BidiIter2"/>
</template><parameter name="lhs"><paramtype><classname>reverse_iterator</classname>&lt; BidiIter1 &gt;</paramtype></parameter><parameter name="rhs"><paramtype><classname>reverse_iterator</classname>&lt; BidiIter2 &gt;</paramtype></parameter></function>
<function name="make_reverse_iterator"><type>auto</type><template>
<template-type-parameter name="BidiIter"/>
</template><parameter name="it"><paramtype>BidiIter</paramtype></parameter><description><para>Makes a <computeroutput><classname alt="boost::stl_interfaces::v1::reverse_iterator">reverse_iterator</classname>&lt;BidiIter&gt;</computeroutput> from an iterator of type <computeroutput>Bidiiter</computeroutput>. </para></description></function>
</namespace>
<namespace name="v2">
<typedef name="reverse_iterator"><description><para>A template alias for <computeroutput>std::reverse_iterator</computeroutput>. This only exists to make migration from Boost.STLInterfaces to C++20 easier; switch to the one in <computeroutput>std</computeroutput> as soon as you can. </para></description><type>std::reverse_iterator&lt; BidiIter &gt;</type></typedef>
<function name="make_reverse_iterator"><type>auto</type><template>
<template-type-parameter name="BidiIter"/>
</template><parameter name="it"><paramtype>BidiIter</paramtype></parameter><description><para>Makes a <computeroutput>reverse_iterator&lt;BidiIter&gt;</computeroutput> from an iterator of type <computeroutput>Bidiiter</computeroutput>. This only exists to make migration from Boost.STLInterfaces to C++20 easier; switch to the one in <computeroutput>std</computeroutput> as soon as you can. </para></description></function>
</namespace>
</namespace>
</namespace>
</header>
<header name="boost/stl_interfaces/sequence_container_interface.hpp">
<namespace name="boost">
<namespace name="stl_interfaces">
<namespace name="v1">
<struct name="sequence_container_interface"><template>
<template-type-parameter name="Derived"/>
<template-nontype-parameter name="Contiguity"><type>element_layout</type></template-nontype-parameter>
</template><description><para>A CRTP template that one may derive from to make it easier to define container types.</para><para>The template parameter <computeroutput>D</computeroutput> for <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput> may be an incomplete type. Before any member of the resulting specialization of <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput> other than special member functions is referenced, <computeroutput>D</computeroutput> shall be complete; shall model <computeroutput>std::derived_from&lt;<classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname>&lt;D&gt;&gt;</computeroutput>, <computeroutput>std::semiregular</computeroutput>, and <computeroutput>std::forward_range</computeroutput>; and shall contain all the nested types required in Table 72: Container requirements and, for those whose iterator nested type models <computeroutput>std::bidirectinal_iterator</computeroutput>, those in Table 73: Reversible container requirements.</para><para>For an object <computeroutput>d</computeroutput> of type <computeroutput>D</computeroutput>, a call to <computeroutput>std::ranges::begin(d)</computeroutput> sxhall not mutate any data members of <computeroutput>d</computeroutput>, and <computeroutput>d</computeroutput>'s destructor shall end the lifetimes of the objects in <computeroutput>[std::ranges::begin(d), std::ranges::end(d))</computeroutput>. </para></description><method-group name="public member functions">
<method name="empty" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin()==std::declval&lt; D &amp; &gt;().end())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="empty" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin()==std::declval&lt; D const &amp; &gt;().end())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="data" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-nontype-parameter name="C"><type>element_layout</type><default>Contiguity</default></template-nontype-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;C == element_layout::contiguous&gt;</default></template-type-parameter>
</template></method>
<method name="data" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-nontype-parameter name="C"><type>element_layout</type><default>Contiguity</default></template-nontype-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;C == element_layout::contiguous&gt;</default></template-type-parameter>
</template></method>
<method name="size" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().end() - std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="size" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().end() - std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="front" cv="noexcept(noexcept(*std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="front" cv="const noexcept(noexcept(*std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="push_front" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().emplace_front(x))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="x"><paramtype>typename D::value_type const &amp;</paramtype></parameter></method>
<method name="push_front" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().emplace_front(std::move(x)))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="x"><paramtype>typename D::value_type &amp;&amp;</paramtype></parameter></method>
<method name="pop_front" cv="noexcept"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="back" cv="noexcept(noexcept(*std::prev(std::declval&lt; D &amp; &gt;().end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt; <classname alt="boost::stl_interfaces::v1::v1_dtl::decrementable_sentinel">v1_dtl::decrementable_sentinel</classname>&lt;D&gt;::value &amp;&amp; v1_dtl::common_range&lt;D&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="back" cv="const noexcept(noexcept(*std::prev(std::declval&lt; D const &amp; &gt;().end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt; <classname alt="boost::stl_interfaces::v1::v1_dtl::decrementable_sentinel">v1_dtl::decrementable_sentinel</classname>&lt;D&gt;::value &amp;&amp; v1_dtl::common_range&lt;D&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="push_back" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().emplace_back(x))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="x"><paramtype>typename D::value_type const &amp;</paramtype></parameter></method>
<method name="push_back" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().emplace_back(std::move(x)))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="x"><paramtype>typename D::value_type &amp;&amp;</paramtype></parameter></method>
<method name="pop_back" cv="noexcept"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="operator[]" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin()[n])))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="n"><paramtype>typename D::size_type</paramtype></parameter></method>
<method name="operator[]" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin()[n])))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="n"><paramtype>typename D::size_type</paramtype></parameter></method>
<method name="at"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="i"><paramtype>typename D::size_type</paramtype></parameter></method>
<method name="at" cv="const"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="i"><paramtype>typename D::size_type</paramtype></parameter></method>
<method name="begin" cv="const noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr Iter</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Iter"><default>typename D::const_iterator</default></template-type-parameter>
</template></method>
<method name="end" cv="const noexcept(noexcept(std::declval&lt; D &amp; &gt;().end())))"><type>constexpr Iter</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Iter"><default>typename D::const_iterator</default></template-type-parameter>
</template></method>
<method name="cbegin" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="cend" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().end())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="rbegin" cv="noexcept(noexcept(stl_interfaces::make_reverse_iterator(std::declval&lt; D &amp; &gt;().end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;v1_dtl::common_range&lt;D&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="rend" cv="noexcept(noexcept(stl_interfaces::make_reverse_iterator(std::declval&lt; D &amp; &gt;().begin()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;v1_dtl::common_range&lt;D&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="rbegin" cv="const noexcept(noexcept(std::declval&lt; D &amp; &gt;().rbegin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="rend" cv="const noexcept(noexcept(std::declval&lt; D &amp; &gt;().rend())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="crbegin" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().rbegin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="crend" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().rend())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="insert" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().emplace(pos, x))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="pos"><paramtype>typename D::const_iterator</paramtype></parameter><parameter name="x"><paramtype>typename D::value_type const &amp;</paramtype></parameter></method>
<method name="insert" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;() .emplace(pos, std::move(x)))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="pos"><paramtype>typename D::const_iterator</paramtype></parameter><parameter name="x"><paramtype>typename D::value_type &amp;&amp;</paramtype></parameter></method>
<method name="insert" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().insert(pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="pos"><paramtype>typename D::const_iterator</paramtype></parameter><parameter name="n"><paramtype>typename D::size_type</paramtype></parameter><parameter name="x"><paramtype>typename D::value_type const &amp;</paramtype></parameter></method>
<method name="insert" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;() .insert(pos, il.begin(), il.end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="pos"><paramtype>typename D::const_iterator</paramtype></parameter><parameter name="il"><paramtype>std::initializer_list&lt; typename D::value_type &gt;</paramtype></parameter></method>
<method name="erase" cv="noexcept"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="pos"><paramtype>typename D::const_iterator</paramtype></parameter></method>
<method name="assign" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().insert(std::declval&lt; D &amp; &gt;().begin(), first, last))))"><type>constexpr auto</type><template>
<template-type-parameter name="InputIterator"/>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;v1_dtl::in_iter&lt;InputIterator&gt;::value&gt;</default></template-type-parameter>
</template><parameter name="first"><paramtype>InputIterator</paramtype></parameter><parameter name="last"><paramtype>InputIterator</paramtype></parameter></method>
<method name="assign" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;() .insert(std::declval&lt; D &amp; &gt;().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="n"><paramtype>typename D::size_type</paramtype></parameter><parameter name="x"><paramtype>typename D::value_type const &amp;</paramtype></parameter></method>
<method name="assign" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().assign(il.begin(), il.end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="il"><paramtype>std::initializer_list&lt; typename D::value_type &gt;</paramtype></parameter></method>
<method name="clear" cv="noexcept"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
</method-group>
<copy-assignment cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().assign(il.begin(), il.end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="il"><paramtype>std::initializer_list&lt; typename D::value_type &gt;</paramtype></parameter></copy-assignment>
</struct><namespace name="v1_dtl">
<struct name="clear_impl"><template>
<template-type-parameter name="D"/>
<template-type-parameter name=""><default>void</default></template-type-parameter>
</template><method-group name="public static functions">
<method name="call" cv="noexcept" specifiers="static"><type>constexpr void</type><parameter name="d"><paramtype>D &amp;</paramtype></parameter></method>
</method-group>
</struct><struct-specialization name="clear_impl"><template>
<template-type-parameter name="D"/>
</template><specialization><template-arg>D</template-arg><template-arg>void_t&lt; decltype(std::declval&lt; D &gt;().clear())&gt;</template-arg></specialization><method-group name="public static functions">
<method name="call" cv="noexcept" specifiers="static"><type>constexpr void</type><parameter name="d"><paramtype>D &amp;</paramtype></parameter></method>
</method-group>
</struct-specialization><typedef name="in_iter"><type>std::is_convertible&lt; typename std::iterator_traits&lt; Iter &gt;::iterator_category, std::input_iterator_tag &gt;</type></typedef>
<function name="derived_container"><type>void</type><template>
<template-type-parameter name="D"/>
<template-nontype-parameter name="Contiguity"><type>element_layout</type></template-nontype-parameter>
</template><parameter name=""><paramtype><classname>sequence_container_interface</classname>&lt; D, Contiguity &gt; const &amp;</paramtype></parameter></function>
</namespace>
<function name="swap"><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface &amp;</paramtype></parameter><description><para>Implementation of free function <computeroutput>swap()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
<function name="operator=="><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><description><para>Implementation of <computeroutput>operator==()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
<function name="operator!="><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><description><para>Implementation of <computeroutput>operator!=()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
<function name="operator&lt;"><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><description><para>Implementation of <computeroutput>operator&lt;()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
<function name="operator&lt;="><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><description><para>Implementation of <computeroutput>operator&lt;=()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
<function name="operator&gt;"><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><description><para>Implementation of <computeroutput>operator&gt;()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
<function name="operator&gt;="><type>constexpr auto</type><template>
<template-type-parameter name="ContainerInterface"/>
</template><parameter name="lhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><parameter name="rhs"><paramtype>ContainerInterface const &amp;</paramtype></parameter><description><para>Implementation of <computeroutput>operator&gt;=()</computeroutput> for all containers derived from <computeroutput><classname alt="boost::stl_interfaces::v1::sequence_container_interface">sequence_container_interface</classname></computeroutput>. </para></description></function>
</namespace>
</namespace>
</namespace>
</header>
<header name="boost/stl_interfaces/view_interface.hpp">
<namespace name="boost">
<namespace name="stl_interfaces">
<namespace name="v1">
<struct name="view_interface"><template>
<template-type-parameter name="Derived"/>
<template-nontype-parameter name="Contiguity"><type>element_layout</type></template-nontype-parameter>
</template><description><para>A CRTP template that one may derive from to make it easier to define <computeroutput>std::ranges::view</computeroutput>-like types with a container-like interface. This is a pre-C++20 version of C++20's <computeroutput><classname alt="boost::stl_interfaces::v1::view_interface">view_interface</classname></computeroutput> (see [view.interface] in the C++ standard).</para><para>The template parameter <computeroutput>D</computeroutput> for <computeroutput><classname alt="boost::stl_interfaces::v1::view_interface">view_interface</classname></computeroutput> may be an incomplete type. Before any member of the resulting specialization of <computeroutput><classname alt="boost::stl_interfaces::v1::view_interface">view_interface</classname></computeroutput> other than special member functions is referenced, <computeroutput>D</computeroutput> shall be complete, and model both <computeroutput>std::derived_from&lt;<classname alt="boost::stl_interfaces::v1::view_interface">view_interface</classname>&lt;D&gt;&gt;</computeroutput> and <computeroutput>std::view</computeroutput>. </para></description><method-group name="public member functions">
<method name="empty" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin()==std::declval&lt; D &amp; &gt;().end())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="empty" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin()==std::declval&lt; D const &amp; &gt;().end())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="operator bool" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().empty())))" specifiers="explicit"><type>constexpr</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="R"><default>decltype(std::declval&lt;D &amp;&gt;().empty())</default></template-type-parameter>
</template></method>
<method name="operator bool" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().empty())))" specifiers="explicit"><type>constexpr</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="R"><default>decltype(std::declval&lt;D const &amp;&gt;().empty())</default></template-type-parameter>
</template></method>
<method name="data" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-nontype-parameter name="C"><type>element_layout</type><default>Contiguity</default></template-nontype-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;C == element_layout::contiguous&gt;</default></template-type-parameter>
</template></method>
<method name="data" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-nontype-parameter name="C"><type>element_layout</type><default>Contiguity</default></template-nontype-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt;C == element_layout::contiguous&gt;</default></template-type-parameter>
</template></method>
<method name="size" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().end() - std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="size" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().end() - std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="front" cv="noexcept(noexcept(*std::declval&lt; D &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="front" cv="const noexcept(noexcept(*std::declval&lt; D const &amp; &gt;().begin())))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template></method>
<method name="back" cv="noexcept(noexcept(*std::prev(std::declval&lt; D &amp; &gt;().end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt; <classname alt="boost::stl_interfaces::v1::v1_dtl::decrementable_sentinel">v1_dtl::decrementable_sentinel</classname>&lt;D&gt;::value &amp;&amp; v1_dtl::common_range&lt;D&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="back" cv="const noexcept(noexcept(*std::prev(std::declval&lt; D const &amp; &gt;().end()))))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
<template-type-parameter name="Enable"><default>std::enable_if_t&lt; <classname alt="boost::stl_interfaces::v1::v1_dtl::decrementable_sentinel">v1_dtl::decrementable_sentinel</classname>&lt;D&gt;::value &amp;&amp; v1_dtl::common_range&lt;D&gt;::value&gt;</default></template-type-parameter>
</template></method>
<method name="operator[]" cv="noexcept(noexcept(std::declval&lt; D &amp; &gt;().begin()[n])))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="n"><paramtype>v1_dtl::range_difference_t&lt; D &gt;</paramtype></parameter></method>
<method name="operator[]" cv="const noexcept(noexcept(std::declval&lt; D const &amp; &gt;().begin()[n])))"><type>constexpr auto</type><template>
<template-type-parameter name="D"><default>Derived</default></template-type-parameter>
</template><parameter name="n"><paramtype>v1_dtl::range_difference_t&lt; D &gt;</paramtype></parameter></method>
</method-group>
</struct><namespace name="v1_dtl">
<function name="derived_view"><type>void</type><template>
<template-type-parameter name="D"/>
<template-nontype-parameter name="Contiguity"><type>element_layout</type></template-nontype-parameter>
</template><parameter name=""><paramtype><classname>view_interface</classname>&lt; D, Contiguity &gt; const &amp;</paramtype></parameter></function>
</namespace>
<function name="operator!="><type>constexpr auto</type><template>
<template-type-parameter name="ViewInterface"/>
</template><parameter name="lhs"><paramtype>ViewInterface</paramtype></parameter><parameter name="rhs"><paramtype>ViewInterface</paramtype></parameter><description><para>Implementation of <computeroutput>operator!=()</computeroutput> for all views derived from <computeroutput><classname alt="boost::stl_interfaces::v1::view_interface">view_interface</classname></computeroutput>. </para></description></function>
</namespace>
<namespace name="v2">
<typedef name="view_interface"><description><para>A template alias for <computeroutput>std::view_interface</computeroutput>. This only exists to make migration from Boost.STLInterfaces to C++20 easier; switch to the one in <computeroutput>std</computeroutput> as soon as you can. </para></description><type>std::ranges::view_interface&lt; D &gt;</type></typedef>
</namespace>
</namespace>
</namespace>
</header>
</library-reference>

View File

@@ -0,0 +1,953 @@
[/
/ 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)
/]
[section Tutorial: `iterator_interface`]
[note All the member functions provided by _iter_iface_ are in your iterator's
base class _emdash_ _iter_iface_ _emdash_ and can therefore be hidden if you
define a member function with the same name in your derived iterator. If you
don't like the semantics of any _iter_iface_-provided member function, feel
free to replace it.]
[heading The `iterator_interface` Template]
Though a given iterator may have a large number of operations associated with
it, there are only a few basis operations that the iterator needs to define;
the full set of operations it supports can be defined in terms of that much
smaller basis.
It is possible to define any iterator `Iter` in terms of a subset of
user-defined operations. By deriving `Iter` from _iter_iface_ using _CRTP_,
we can generate the full set of operations. Here is the declaration of
_iter_iface_:
template<
typename Derived,
typename IteratorConcept,
typename ValueType,
typename Reference = ValueType &,
typename Pointer = ValueType *,
typename DifferenceType = std::ptrdiff_t>
struct iterator_interface;
Let's break that down.
`Derived` is the type that you're deriving _iter_iface_ from.
`IteratorConcept` defines the iterator category/concept. This must be one of
the C++ standard iterator tag types, like `std::forward_iterator_tag`. In
C++20 and later, `std::contiguous_iterator_tag` is a valid tag to use.
`ValueType` is used to define the iterator's `value_type` typedef. Likewise,
`Reference` and `Pointer` are used to define the iterator's `reference` and
`pointer` typedefs, respectively.
[tip `Reference` does not need to be a reference type, and `Pointer` does not
need to be a pointer type. This fact is very useful when making proxy
iterators. ]
`DifferenceType` is used to define the iterator's `difference_type`. Don't be
a weirdo; just leave this as the default type, `std::ptrdiff_t`.
[heading Proxy Iterators]
Sometimes you need to create an iterator `I` such that `I::reference_type` is
not a (possibly `const`) reference to `I::value_type`. For instance, let's
say you want to make a zip-iterator that produces pairs of values from two
separate underlying sequences. For sequences `A` and `B`, with respective
`value_type`s `T` and `U`, one possible `reference_type` for a zip iterator
would be `std::pair<T &, U &>` (this is distinct from a reference to a pair,
such as `std::pair<T, U> &`). Each such pair would contain a reference to one
element from `A` and a reference to the corresponding element from `B`.
As another example, if you wanted an iterator `I` that represents the infinite
sequence 0, 1, 2, ..., you'd be unable to form a reference to most or all of
those values; you'd instead produce a temporary for each value as it is
needed. This implies that `I::value_type` does not involve references at all;
it may instead by `int` or `double`.
When defining a proxy iterator, you can use a template alias that provides
reasonable defaults for _iter_iface_'s parameters:
template<
typename Derived,
typename IteratorConcept,
typename ValueType,
typename Reference = ValueType,
typename DifferenceType = std::ptrdiff_t>
using proxy_iterator_interface = iterator_interface<
Derived,
IteratorConcept,
ValueType,
Reference,
proxy_arrow_result<Reference>,
DifferenceType>;
[note As shown above, _proxy_iter_iface_ uses a template called
`proxy_arrow_result` as its pointer-type. This template makes a copy of
whatever value is obtained by `operator*`, and then returns a pointer to the
copy in its `operator->`. You may want to use something else if this is a
performance concern. ]
[heading User-Defined Iterator Operations]
Now, let's get back to the user-defined basis operations.
In the table below, `Iter` is a user-defined type derived from _iter_iface_;
`i` and `i2` are objects of type `Iter`; `reference` is the type passed as the
`Reference` template parameter to _iter_iface_; `pointer` is the type passed
as the `Pointer` template parameter to _iter_iface_; and `n` is a value of
type `difference_type`.
[table User-Defined Operations
[[Expression] [Return Type] [Semantics] [Assertion/note/pre-/post-condition]]
[
[ `*i` ]
[ Convertible to `reference`. ]
[ Dereferences `i` and returns the result. ]
[ ['Expects:] i is dereferenceable. ]
]
[
[ `i == i2` ]
[ Contextually convertible to `bool`. ]
[ Returns true if and only if `i` and `i2` refer to the same value. ]
[ ['Expects:] `(i, i2)` is in the domain of `==`. ]
]
[
[ `i2 - i` ]
[ Convertible to `difference_type`. ]
[ Returns `n`. ]
[ ['Expects:] there exists a value `n` of type `difference_type` such that `i + n == i2`.
`i2 == i + (i2 - i)`. ]
]
[
[ `++i` ]
[ `Iter &` ]
[ Increments `i`. ]
[ ]
]
[
[ `--i` ]
[ `Iter &` ]
[ Decrements `i`. ]
[ ]
]
[
[ `i += n` ]
[ `Iter &` ]
[
``difference_type m = n;
if (m >= 0)
while (m--) ++i;
else
while (m++) --i;`` ]
[ ]
]
]
[tip The table above leaves a lot of implementation freedom. In
`operator+=()`, you could take `n` as a value or as a reference; `operator-()`
can return a `difference_type` or just something convertible to one; etc. In
particular, your operations can be `constexpr` or `noexcept` as you see fit.]
Not all the iterator concepts require all the operations above. Here are the
operations used with each iterator concept:
[table Operations Required for Each Concept
[[Concept] [Operations]]
[
[ `input_iterator` ]
[ ``*i
i == i2
++i`` ]
]
[
[ `output_iterator` ]
[ ``*i
++i`` ]
]
[
[ `forward_iterator` ]
[ ``*i
i == i2
++i`` ]
]
[
[ `bidirectional_iterator` ]
[ ``*i
i == i2
++i
--i`` ]
]
[
[ `random_access_iterator`/`continguous_iterator` ]
[ ``*i
i - i2
i += n`` ]
]
]
[note For `random_access_iterator`s, the operation `i - i2` is used to provide
all the relational operators, including `operator==()` and `operator!=()`. If
you are defining an iterator over a discontiguous sequence
(e.g. `std::deque`), this implementation of `operator==()` may not be optimal.
In this case, provide your own `operator==()`. `operator!=()` will be
provided if `operator==` is available. ]
[heading An Important Note About `operator++()` and `operator--()`]
There's a wrinkle in this way of doing things. When you define `operator++()`
in your iterator type `Derived`, _iter_iface_ defines post-increment,
`operator++(int)`. But since `Derived` has an `operator++` and so does its
base class _iter_iface_, the one in `Derived` *hides* the one in _iter_iface_.
So, you need to add a using declaration that makes the `operator++` from the
base class visible in the derived class. For instance, in the `node_iterator`
example there are these lines:
[node_iterator_using_declaration]
[important All of the above applies to `operator--`. So, for bidirectional
iterators, you need to add a line like `using base_type::operator--;` as
well. ]
[note These using declarations are not necessary for a random access iterator,
because `Derived` does not have an `operator++()` in that case. ]
[heading Putting it All Together]
Ok, let's actually define a simple iterator. Let's say you need to interact
with some legacy code that has a hand-written linked list:
[node_defn]
We can't change this code to use `std::list`, but it would be nice to be able
to reuse all of the standard algorithms with this type. Defining an iterator
will get us there.
[node_iterator_class_head]
We are deriving `node_iterator` from _iter_iface_, and because we're using
_CRTP_, we first have to pass `node_iterator` for the `Derived` template
parameter, so that _iter_iface_ knows what derived type to cast to in order to
get at the user-defined operations. Then, we pass `std::forward_iterator_tag`
for `IteratorConcept`, since that's appropriate for a singly-linked list.
Finally, we pass `T` to let _iter_iface_ know what the `value_type` is for our
iterator.
We leave the rest of the template parameters at their defaults: `T &` for
`Reference`, `T *` for `Pointer`, and `std::ptrdiff_t` for `DifferenceType`.
This is what you will do for almost all iterators. The most common exceptions
to this are usually some kind of proxy iterator. Another exception is when
for better code generation you want to return builtin values instead of
references for constant iterators. To see an example of the latter, see the
`repeated_chars_iterator` in the introduction; it's `Reference` template
parameter is `char` for this reason.
[node_iterator_ctors]
Next, we define two constructors: a default constructor, and one that takes a
`node` pointer. A default constructor is required by the `forward_iterator`
concept, but _iter_iface_ cannot supply this, since constructors are not
visible in derived types without user intervention.
[important A default constructor is required for every iterator concept.]
[node_iterator_user_ops]
Next, we define the user-defined operations that _iter_iface_ requires to do
its work. As you might expect, the three required operations are very
straightforward.
[note Here, I implement `operator==()` as a hidden friend function. it would
have worked just as well if I had instead implemented it as a member function,
like this:
``constexpr bool operator==(node_iterator rhs) const noexcept
{
return it_ == rhs.it_;
}``
Either of these forms works, since _iter_iface_ is concept-based _emdash_ the
appropriate expressions need to be well-formed for the _iter_iface_ tempalte
to do its work. ]
Finally, we need a using declaration to make
`iterator_interface::operator++(int)` visible:
[node_iterator_using_declaration]
Here's how we might use the forward iterator we just defined:
[node_iterator_usage]
[heading What About Adapting an Existing Iterator?]
So glad you asked. If you want to make something like a filtering iterator,
or say a UTF-8 to UTF-32 transcoding iterator, you are starting with an
existing iterator and adapting it. There's a way to avoid having to write all
of the user-defined basis functions, as long as there's a base iterator that
already has the right operations with the right semantics.
For example, consider an iterator that contains a pointer to an array of
`int`, and predicate of type `Pred`. It filters out integers that do not meet
the predicate. Since we are using an existing iterator (the pointer to
`int`), we already have all the operations we need for a bidirectional
iterator (and more), except that `operator++` on an `int *` does not skip over
elements as we'd like. Here's the code:
[filtered_int_iterator_defn]
So, all we had to do was let _iter_iface_ know that there was an underlying
iterator it could use _emdash_ by implementing `base_reference()` _emdash_ and
the operations that we did not define got defined for us by _iter_iface_.
Here is the iterator in action:
[filtered_int_iterator_usage]
[heading Checking Your Work]
_IFaces_ is able to check that some of the code that you write is compatible
with the concept for the iterator you're writing. It cannot check everything.
For instance, _IFaces_ does not know if your derived type includes a default
constructor, which is required by all the iterators. In particular,
_iter_iface_ cannot `static_assert` on the wellformedness of `Derived()`,
since `Derived` is an incomplete type within the body of _iter_iface_
_emdash_ _iter_iface_ is the base class for `Derived`, not the other way
round.
Since you can easily `static_assert` that a type models a given concept, a
good practice is to put such a `static_assert` after you define your iterator
type.
For instance, after `node_iterator` you'll find this code:
[node_iterator_concept_check]
Consider this good code hygiene. Without this simple check, you'll probably
eventually find yourself looking at an error message with a very long template
instantiation stack.
There's also a macro that can help you check that `std::iterator_traits` is
well-formed and provides the correct types. See _traits_m_.
[endsect]
[section Tutorial: `view_interface`]
[note All the member functions provided by _view_iface_ are in your view's
base class _emdash_ _view_iface_ _emdash_ and can therefore be hidden if you
define a member function with the same name in your derived view. If you
don't like the semantics of any _view_iface_-provided member function, feel
free to replace it.]
[heading The `view_interface` Template]
C++20 contains a _CRTP_ template, `std::ranges::view_interface`, which takes a
range or view, and adds all the operations that view types have, using only
the range's/view's `begin()` and `end()`. This is a C++14-compatible version
of that template.
As with _iter_iface_, _view_iface_ makes it possible to write very few
operations _emdash_ only `begin()` and `end()` are actually used by
_view_iface_ _emdash_ and get all the other operations that go with view
types. The operations added depend on what kinds of iterator and/or sentinel
types your derived view type defines.
Here is the declaration of _view_iface_:
template<typename Derived, element_layout Contiguity = element_layout::discontiguous>
struct view_interface;
_view_iface_ only requires the derived type and an optional non-type template
parameter that indicates whether `Derived`'s iterators are contiguous. The
non-type parameter is necessary to support pre-C++20 code.
[note Proxy iterators are inherently discontiguous.]
In this example, we're implementing something very similar to
`std::ranges::drop_while_view`. First, we need helper view types `subrange`
and `all_view`, and a function that takes a range and returns a view of the
entire range, `all()`:
[all_view]
Note that `subrange` is derived from _view_iface_, so it will have all the
view-like operations that are appropriate to its `Iterator` and `Sentinel`
types.
With the helpers available, we can define `drop_while_view`:
[drop_while_view_template]
Now, let's look at code using these types, including operations defined by
_view_iface_ that we did not have to write:
[drop_while_view_usage]
If you want more details on _view_iface_, you can find it wherever you usually
find reference documentation on the standard library. We won't cover it here
for that reason. See [@http://eel.is/c++draft/view.interface [view.interface]
on eel.is] or [@https://cppreference.com] for details.
[endsect]
[section Tutorial: `sequence_container_interface`]
[note All the member functions provided by _cont_iface_ are in your
container's base class _emdash_ _cont_iface_ _emdash_ and can therefore be
hidden if you define a member function with the same name in your derived
container. If you don't like the semantics of any _cont_iface_-provided
member function, feel free to replace it.]
[heading The `sequence_container_interface` Template]
As mentioned earlier, writing containers is very tedious. The container
requirements tables in the C++ standard are long and complicated, and there
are a lot of them. The requirements often call for multiple overloads of a
function, all of which could be implemented in terms of just one overload.
There is a large development cost associated with implementing a
standard-compliant container. As a result very few people do so.
_cont_iface_ exists to make bring that large development time way, way down.
Here is its declaration:
template<typename Derived, element_layout Contiguity = element_layout::discontiguous>
struct sequence_container_interface;
Just as with _view_iface_, _cont_iface_ takes the derived type and an optional
non-type template parameter that indicates whether `Derived`'s iterators are
contiguous. The non-type parameter is necessary to support pre-C++20 code.
[heading How `sequence_container_interface` is Organized]
The tables below represent a subset of the operations needed for each of the
container requirements tables in the standard. Here are the tables that apply
to sequence containers (from `[container.requirements]` in the standard):
* Container requirements `[tab:container.req]`
* Reversible container requirements `[tab:container.rev.req]`
* Optional container operations `[tab:container.opt]`
* Allocator-aware container requirements `[tab:container.alloc.req]`
* Sequence container requirements `[tab:container.seq.req]`
* Optional sequence container operations `[tab:container.seq.opt]`
Each requirements table lists all the types and operations required by a
standard-conforming container. All of these sets of requirements are
supported by _cont_iface_, except the allocator-aware container requirements.
The container and sequence container requirements are required for any
sequence container. _cont_iface_ provides each member in any table above
(again, except the allocator-aware ones). Each member is individually
constrained, so if a given member (say, a particular `insert()` overload) is
ill-formed, it will not be usable in the resulting container.
[note All table requirements satisfied by _IFaces_ use the 2017 version of the
C++ Standard. See your favorite online resource for the contents of these
tables. Many people like [@http://eel.is/c++draft eel.is], which is really
easy to navigate.]
Note that _cont_iface_ does not interact at all with the allocator-aware
container requirements, the associative container requirements, or the
unordered associative container requirements. Specifically, nothing precludes
you from satisfying any of those sets or requirements _emdash_ it's just that
_cont_iface_ does not.
[heading How `sequence_container_interface` Works]
To use _cont_iface_, you provide certain operations yourself, and _cont_iface_
fills in the rest. If any provided operation `O` is not to your liking
_emdash_ say, because you know of a way to do `O` directly in a way that is
more efficient than the way that _cont_iface_ does it _emdash_ you can
implement `O` yourself. Since your implementation is in a class `D` derived
from _cont_iface_, it will hide the `O` from _cont_iface_.
Below, there are tables that show what user-defined types and operations are
required for _cont_iface_ to fulfill all the requirements from one of the C++
Standard's requirements tables. For instance, the table "Optional
User-Defined Types and Operations for Containers" below shows what you need to
provide to fulfill all the requirements in the standard's "Container
requirements `[tab:container.req]`" table.
So, to use _cont_iface_ to make a `std::array`-like container (which is not a
sequence container, because it has no `insert()`, `erase()`, etc.), you need
to define the types and operations in the "User-Defined Types and Operations
for Containers" table, and optionally the ones in the "Optional User-Defined
Types and Operations for Containers".
To make a `std::forward_list`-like type, you need to define the types and
operations in the "User-Defined Types and Operations for Containers" table,
and optionally the ones in the "Optional User-Defined Types and Operations for
Containers". You would also define the types and operations in the
"User-Defined Types and Operations for Sequence Containers" table. You cannot
define the types and operations in the "User-Defined Types and Operations for
Reversible Containers" table, because your container is forward-only.
To make a `std::vector`-like type, you would provide the types and operations
in all the tables below.
If you have a type that does not have all the operations in one of the tables,
that's fine -- you can just implement the operations that your type can do,
and whatever operations can be provided by _cont_iface_ in terms of the
user-defined operations, will be provided. For example, the `std::array`-like
container described above would have `front()` _emdash_ which comes from the
optional sequence container requirements _emdash_ even if you did not write
any user-defined insertion or erasure member functions into your container.
If it has bidirectional iterators, the `std::array`-like container will have
`back()` too.
[heading The `sequence_container_interface` Tables]
After each requirements table, there's a table indicating how _cont_iface_
maps the user-defined operations to the operations it provides. These mapping
tables can be handy if you have a container that meets only some of the
requirements of one of the requirements tables.
In the tables, `X` is a user-defined type derived from _cont_iface_ containing
objects of type `T`; `a` and `b` are objects of type `X`; `i` and `j` are
objects of type (possibly const) `X::iterator`; `u` is an identifier; `r` is a
non-const value of type `X`; `rv_c` is a non-const rvalue of type `X`; `i` and
`j` are forward iterators that refer to elements implicitly convertible to
`T`; `[i, j)` is a range; `il` is an object of type
`std::initializer_list<T>`; `n` is a value of type `X::size_type`, `p` is a
valid constant iterator to `a`; `q` is a valid dereferenceable constant
iterator to `a`; `[q1, q2)` is a valid range of constant iterators to `a`; `t`
is an lvalue or a const rvalue of T; and `rv` denotes a non-const rvalue of
`T`. `Args` is a template parameter pack; `args` denotes a function parameter
pack with the pattern `Args &&`.
[heading Container]
All containers must meet the requirements of this table:
[table User-Defined Types and Operations for Containers
[[Expression] [Return Type] [Semantics] [Assertion/note/pre-/post-condition]]
[
[ `X::value_type` ]
[ `T` ]
[ ]
[ Compile time only. ]
]
[
[ `X::reference` ]
[ `T &` ]
[ ]
[ Compile time only. ]
]
[
[ `X::const_reference` ]
[ `T const &` ]
[ ]
[ Compile time only. ]
]
[
[ `X::iterator` ]
[ An iterator whose `value_type` is `T`. ]
[ ]
[ Must meet the forward iterator requirements, and must be convertible to `X::const_iterator`. Compile time only. ]
]
[
[ `X::const_iterator` ]
[ A constant iterator whose `value_type` is `T`. ]
[ ]
[ Must meet the forward iterator requirements. Compile time only. ]
]
[
[ `X::difference_type` ]
[ A signed integer type. ]
[ ]
[ Identical to the diference type of `X::iterator` and `X::const_iterator`. Compile time only. ]
]
[
[ `X::size_type` ]
[ An unsigned integer type. ]
[ ]
[ Compile time only. ]
]
[
[ `X u;` ]
[ ]
[ ]
[ ['Ensures:] `u.empty()` ]
]
[
[ ``X u(a);
X u = a;
`` ]
[ ]
[ ]
[ ['Ensures:] `u == a` ]
]
[
[ ``X u(rv);
X u = rv;
`` ]
[ ]
[ ]
[ ['Ensures:] `u` is equal to the value `rv_c` had before this operation. ]
]
[
[ `a = rv` ]
[ `X &` ]
[ All existing elements of `a` are either move assigned to or destroyed. ]
[ ['Ensures:] `u` is equal to the value `rv_c` had before this operation. ]
]
[
[ `a.~X()` ]
[ ]
[ Destroys every element of `a`; any memory obtained is deallocated. ]
[ ]
]
[
[ `a.begin()` ]
[ `X::iterator` ]
[ ]
[ This is the non-`const` overload; the `const` overload is not needed. ]
]
[
[ `a.end()` ]
[ `X::iterator` ]
[ ]
[ This is the non-`const` overload; the `const` overload is not needed. ]
]
[
[ `a.swap(b)` ]
[ Convertible to `bool`. ]
[ Exchanges the contents of `a` and `b`. ]
[ ]
]
[
[ `r = a` ]
[ `X &` ]
[ ]
[ ['Ensures:] `r` == `a`. ]
]
[
[ `a.max_size()` ]
[ `X::size_type` ]
[ `std::distance(l.begin(), l.end())` for the largest possible container `l`. ]
[ ]
]
]
[note The requirements above are taken from the standard. Even though the
standard requires things to be a certain way, you can often define types that
work in any context in which a container is supposed to work, even though it
varies from the requirements above. In particular, you may want to have
non-reference and non-pointer types for `X::reference` and `X::pointer`,
respectively _emdash_ and that certainly will not break _cont_iface_. ]
If you provide the types and operations above, _cont_iface_ will provide the
rest of the container requirements, using this mapping:
[table User-Defined Operations to sequence_container_interface Operations
[[User-Defined] [_cont_iface_-Provided] [Note]]
[
[ ``a.begin()
a.end()`` ]
[ ``a.empty()
a.size()
a.begin()
a.end()
a.cbegin()
a.cend()`` ]
[ The user-defined `begin()` and `end()` are non-`const`, and the _cont_iface_-provided ones are `const`. _cont_iface_ can only provide `size()` if `X::const_iterator` is a random access iterator; otherwise, it must be user-defined. ]
]
[
[ `a == b` ]
[ `a != b` ]
[ Though `a == b` is provided by _cont_iface_, any user-defined replacement will be used to provide `a != b`. ]
]
[
[ `a.swap(b)` ]
[ `swap(a, b)` ]
[ ]
]
]
[heading Reversible Container]
Containers that are reverse-iterable must meet the requirements of this table
(in addition to the container requirements):
[table User-Defined Types and Operations for Reversible Containers
[[Expression] [Return Type] [Semantics] [Assertion/note/pre-/post-condition]]
[
[ `X::reverse_iterator` ]
[ `boost::stl_interfaces::reverse_iterator<X::iterator>` ]
[ ]
[ Compile time only. ]
]
[
[ `X::const_reverse_iterator` ]
[ `boost::stl_interfaces::reverse_iterator<X::const_iterator>` ]
[ ]
[ Compile time only. ]
]
]
If you provide the types and operations above, _cont_iface_ will provide the
rest of the reversible container requirements, using this mapping:
[table User-Defined Operations to sequence_container_interface Operations
[[User-Defined] [_cont_iface_-Provided] [Note]]
[
[ ``a.begin()
a.end()`` ]
[ ``a.rbegin()
a.rend()
a.crbegin()
a.crend()`` ]
[ The user-defined `begin()` and `end()` are non-`const`, and _cont_iface_ provides both `const` and non-`const` overloads of `rbegin()` and `rend()`. _cont_iface_ can only provide these operations if `X::iterator` and `X::const_iterator` are bidirectional iterators. ]
]
]
[heading Optional Container Operations]
Containers that are comparable with `<`, `>`, `<=`, and `>=` get those
operations automatically, so long as `T` is less-than comparable. In this
case, there are no required user-defined operations, so that table is not
needed.
_cont_iface_ will provide the optional container requirements using this
mapping:
[table User-Defined Operations to sequence_container_interface Operations
[[User-Defined] [_cont_iface_-Provided] [Note]]
[
[ `a < b` ]
[ ``a <= b
a > b
a >= b`` ]
[ Though `a < b` is provided by _cont_iface_, any user-defined replacement will be used to provide the other operations listed here. ]
]
]
[heading Sequence Container]
Sequence containers meet the requirements of this table (in addition to the
container requirements):
[table User-Defined Types and Operations for Sequence Containers
[[Expression] [Return Type] [Semantics] [Assertion/note/pre-/post-condition]]
[
[ `X u(n, t);` ]
[ ]
[ Constructs a sequence of `n` copies of `t`. ]
[ ['Ensures:] `distance(u.begin(), u.end()) == n` ]
]
[
[ `X u(i, j);` ]
[ ]
[ Constructs a sequence equal to `[i, j)`. ]
[ ['Ensures:] `distance(u.begin(), u.end()) == distance(i, j)` ]
]
[
[ `X u(il);` ]
[ ]
[ `X u(il.begin(), il.end());` ]
[ ]
]
[
[ `a.emplace(p, args)` ]
[ `X::iterator` ]
[ Inserts an object of type T constructed with `std::forward<Args>(args)...` before `p`. ]
[ `args` may directly or indirectly refer to a value in `a`. ]
]
[
[ `a.insert(p, i, j)` ]
[ `X::iterator` ]
[ Inserts copies of the elements in `[i, j)` before `p`. ]
[ ]
]
[
[ `a.erase(q1, q2)` ]
[ `X::iterator` ]
[ Erases the elements in the range `[q1, q2)`. ]
[ ]
]
]
[important In the notes for `a.emplace(p, args)`, it says: "`args` may
directly or indirectly refer to a value in `a`". Don't forget to handle that
case in your implementation. Otherwise, `a.emplace(a.begin(), a.back())` may
do the wrong thing.]
If you provide the types and operations above, _cont_iface_ will provide the
rest of the sequence container requirements, using this mapping:
[table User-Defined Operations to sequence_container_interface Operations
[[User-Defined] [_cont_iface_-Provided] [Note]]
[
[ `X(il)` ]
[ `a = il` ]
[ ]
]
[
[ `a.emplace(p, args)` ]
[
``a.insert(p, t)
a.insert(p, rv)`` ]
[ ]
]
[
[ `a.insert(p, i, j)` ]
[ ``a.insert(p, n, t)
a.insert(p, il)`` ]
[ ]
]
[
[ `a.erase(q1, q2)` ]
[ ``a.erase(q)
a.clear()`` ]
[ ]
]
[
[ ``a.erase(q1, q2)
a.insert(p, i, j)`` ]
[ ``a.assign(i, j)
a.assign(n, t)
a.assign(il)`` ]
[ `a.erase(q1, q2)` and `a.insert(p, i, j)` must both be user-defined for _cont_iface_ to provide these operations. ]
]
]
[heading Optional Sequence Container Operations]
Sequence containers with `front()`, `back()`, or any of the other operations
in this table must define these operations (in addition to the container
requirements):
[table User-Defined Types and Operations for Sequence Containers
[[Expression] [Return Type] [Semantics] [Assertion/note/pre-/post-condition]]
[
[ `a.emplace_front(args)` ]
[ `X::reference` ]
[ Prepends an object of type `T` constructed with `std::forward<Args>(args)...`. ]
[ ]
]
[
[ `a.emplace_back(args)` ]
[ `X::reference` ]
[ Appends an object of type `T` constructed with `std::forward<Args>(args)...`. ]
[ ]
]
]
If you provide the types and operations above, _cont_iface_ will provide the
rest of the optional sequence container requirements, using this mapping:
[table User-Defined Operations to sequence_container_interface Operations
[[User-Defined] [_cont_iface_-Provided] [Note]]
[
[ `a.begin()` ]
[ ``a.front()
a[n]
a.at(n)
`` ]
[ These operations are provided in `const` and non-`const` overloads. _cont_iface_ can only provide `a[n]` and `a.at(n)` if `X::iterator` and `X::const_iterator` are random access iterators. ]
]
[
[ `a.end()` ]
[ `a.back()` ]
[ `back()` is provided in `const` and non-`const` overloads. _cont_iface_ can only provide `a.back()` if `X::iterator` and `X::const_iterator` are bidirectional iterators. ]
]
[
[ `a.emplace_front(args)` ]
[ ``a.push_front(t)
a.push_front(rv)
`` ]
[ ]
]
[
[ `a.emplace_back(args)` ]
[ ``a.push_back(t)
a.push_back(rv)
`` ]
[ ]
]
[
[ ``a.emplace_front(args)
a.erase(q1, q2)``]
[ `a.pop_front(t)` ]
[ `a.emplace_front(args)` and `a.erase(q1, q2)` must both be user-defined for _cont_iface_ to provide this operation. ]
]
[
[ ``a.emplace_back(args)
a.erase(q1, q2)``]
[ `a.pop_back(t)` ]
[ `a.emplace_front(args)` and `a.erase(q1, q2)` must both be user-defined for _cont_iface_ to provide this operation. ]
]
]
[note `emplace_front()` and `emplace_back()` are not needed for some of the
_cont_iface_-provided operations above (e.g. `pop_front()` and `pop_back()`,
respectively). However, they are each used as the user-defined operation that
indicates that the container being defined is front- or
back-mutation-friendly. ]
[heading General Requirements on All User-Defined Operations]
There are other requirements listed in the standard that do not appear in any
of the requirements tables; user-defined operations must conform to those as
well:
* If an exception is thrown by an `insert()` or `emplace()` call while
inserting a single element, that function has no effect.
* No `erase()` function throws an exception.
* No copy constructor or assignment operator of a returned iterator throws an
exception.
* The iterator returned from `a.emplace(p, args)` points to the new element
constructed from `args` into `a`.
* The iterator returned from `a.insert(p, i, j)` points to the copy of the
first element inserted into `a`, or `p` if `i == j`.
* The iterator returned by `a.erase(q1, q2)` points to the element pointed to
by `q2` prior to any elements being erased. If no such element exists,
`a.end()` is returned.
[heading Example: `static_vector`]
Let's look at an example. _Container_ contains a template called
`boost::container::static_vector`, which is a fixed-capacity vector that does
not allocate from the heap. We have a similar template in this example,
`static_vector`. It is implemented by deriving from _cont_iface_, which
provides much of the API specified in the STL, based on a subset of the API
that the user must provide.
`static_vector` meets all the sequence container requirements (including many
of the optional ones) and reversible container requirements in the standard.
It does not meet the allocator-aware container requirements, since it does not
allocate. In short, it has the same full API as `std::vector`, without all
the allocatory bits.
[static_vector_defn]
That's quite a bit of code. However, by using _cont_iface_, we were able to
write only 22 functions, and let _cont_iface_ provide the other 39. 9 of the
22 function that we did have to write were constructors and special member
functions, and those always have to be written in the derived class;
_cont_iface_ never could have helped with those.
[note _cont_iface_ does not support all the sets of container requirements in
the standard. In particular, it does not support the allocator-aware
requirements, and it does not support the associative or unordered associative
container requirements.]
[endsect]
[section Tutorial: `reverse_iterator`]
There is a `std::reverse_iterator` template that has been around since C++98.
In C++20 it is compatible with proxy iterators, and is `constexpr`- and
`noexcept`-friendly. If you are using C++20 or later, just use
`std::reverse_iterator`. For code built against earlier versions of C++, you
can use _rev_iter_.
There's nothing much to document about it; it works just like
`std::reverse_iterator`.
[endsect]

View File

@@ -0,0 +1,35 @@
# Copyright (C) 2019 T. Zachary Laine
#
# 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)
include_directories(${CMAKE_HOME_DIRECTORY})
include(CTest)
enable_testing()
add_custom_target(run_examples COMMAND ${CMAKE_CTEST_COMMAND} -VV -C ${CMAKE_CFG_INTDIR})
macro(add_sample name)
add_executable(${name} ${name}.cpp)
target_link_libraries(${name} stl_interfaces)
set_property(TARGET ${name} PROPERTY CXX_STANDARD ${CXX_STD})
add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name})
if (clang_on_linux)
target_link_libraries(${name} c++)
endif ()
endmacro()
add_sample(repeated_chars_iterator)
add_sample(filtered_int_iterator)
add_sample(node_iterator)
add_sample(random_access_iterator)
add_sample(interoperability)
add_sample(zip_proxy_iterator)
add_sample(back_insert_iterator)
add_sample(reverse_iterator)
add_sample(drop_while_view)
add_sample(static_vector)

View File

@@ -0,0 +1,79 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
//[ back_insert_iterator
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <vector>
#include <cassert>
// This is an output iterator that holds a reference to a container, and calls
// push_back() on that container when it is written to, just like
// std::back_insert_iterator. This is not a lot less code to write than the
// implementation of std::back_insert_iterator, but it demonstrates that
// iterator_interface is flexible enough to handle this odd kind of iterator.
template<typename Container>
struct back_insert_iterator : boost::stl_interfaces::iterator_interface<
back_insert_iterator<Container>,
std::output_iterator_tag,
typename Container::value_type,
back_insert_iterator<Container> &>
{
// For concept requirements.
back_insert_iterator() : c_(nullptr) {}
// Construct from a container.
explicit back_insert_iterator(Container & c) : c_(std::addressof(c)) {}
// When writing to *this, copy v into the container via push_back().
back_insert_iterator & operator=(typename Container::value_type const & v)
{
c_->push_back(v);
return *this;
}
// When writing to *this, move v into the container via push_back().
back_insert_iterator & operator=(typename Container::value_type && v)
{
c_->push_back(std::move(v));
return *this;
}
// Dereferencing *this just returns a reference to *this, so that the
// expression *it = value uses the operator=() overloads above.
back_insert_iterator & operator*() { return *this; }
// There is no underlying sequence over which we are iterating, so there's
// nowhere to go in next(). Do nothing.
back_insert_iterator & operator++() { return *this; }
using base_type = boost::stl_interfaces::iterator_interface<
back_insert_iterator<Container>,
std::output_iterator_tag,
typename Container::value_type,
back_insert_iterator<Container> &>;
using base_type::operator++;
private:
Container * c_;
};
// A convenience function that creates a back_insert_iterator<Container>.
template<typename Container>
back_insert_iterator<Container> back_inserter(Container & c)
{
return back_insert_iterator<Container>(c);
}
int main()
{
std::vector<int> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
std::vector<int> ints_copy;
std::copy(ints.begin(), ints.end(), ::back_inserter(ints_copy));
assert(ints_copy == ints);
}
//]

View File

@@ -0,0 +1,137 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/view_interface.hpp>
#include <algorithm>
#include <vector>
#include <cassert>
//[ all_view
// A subrange is simply an iterator-sentinel pair. This one is a bit simpler
// than the one in std::ranges; its missing a bunch of constructors, prev(),
// next(), and advance().
template<typename Iterator, typename Sentinel>
struct subrange
: boost::stl_interfaces::view_interface<subrange<Iterator, Sentinel>>
{
subrange() = default;
constexpr subrange(Iterator it, Sentinel s) : first_(it), last_(s) {}
constexpr auto begin() const { return first_; }
constexpr auto end() const { return last_; }
private:
Iterator first_;
Sentinel last_;
};
// std::view::all() returns one of several types, depending on what you pass
// it. Here, we're keeping it simple; all() always returns a subrange.
template<typename Range>
auto all(Range && range)
{
return subrange<decltype(range.begin()), decltype(range.end())>(
range.begin(), range.end());
}
// A template alias that denotes the type of all(r) for some Range r.
template<typename Range>
using all_view = decltype(all(std::declval<Range>()));
//]
//[ drop_while_view_template
// Perhaps its clear now why we defined subrange, all(), etc. above.
// drop_while_view contains a view data member. If we just took any old range
// that was passed to drop_while_view's constructor, we'd copy the range
// itself, which may be a std::vector. So, we want to make a view out of
// whatever Range we're given so that this copy of an owning range does not
// happen.
template<typename Range, typename Pred>
struct drop_while_view
: boost::stl_interfaces::view_interface<drop_while_view<Range, Pred>>
{
using base_type = all_view<Range>;
drop_while_view() = default;
constexpr drop_while_view(Range & base, Pred pred) :
base_(all(base)),
pred_(std::move(pred))
{}
constexpr base_type base() const { return base_; }
constexpr Pred const & pred() const noexcept { return pred_; }
// A more robust implementation should probably cache the value computed
// by this function, so that subsequent calls can just return the cached
// iterator.
constexpr auto begin()
{
// We're forced to write this out as a raw loop, since no
// std::-namespace algorithms accept a sentinel.
auto first = base_.begin();
auto const last = base_.end();
for (; first != last; ++first) {
if (!pred_(*first))
break;
}
return first;
}
constexpr auto end() { return base_.end(); }
private:
base_type base_;
Pred pred_;
};
// Since this is a C++14 and later library, we're not using CTAD; we therefore
// need a make-function.
template<typename Range, typename Pred>
auto make_drop_while_view(Range & base, Pred pred)
{
return drop_while_view<Range, Pred>(base, std::move(pred));
}
//]
int main()
{
//[ drop_while_view_usage
std::vector<int> const ints = {2, 4, 3, 4, 5, 6};
// all() returns a subrange, which is a view type containing ints.begin()
// and ints.end().
auto all_ints = all(ints);
// This works using just the used-defined members of subrange: begin() and
// end().
assert(
std::equal(all_ints.begin(), all_ints.end(), ints.begin(), ints.end()));
// These are available because subrange is derived from view_interface.
assert(all_ints[2] == 3);
assert(all_ints.size() == 6u);
auto even = [](int x) { return x % 2 == 0; };
auto ints_after_even_prefix = make_drop_while_view(ints, even);
// Available via begin()/end()...
assert(std::equal(
ints_after_even_prefix.begin(),
ints_after_even_prefix.end(),
ints.begin() + 2,
ints.end()));
// ... and via view_interface.
assert(!ints_after_even_prefix.empty());
assert(ints_after_even_prefix[2] == 5);
assert(ints_after_even_prefix.back() == 6);
//]
}

View File

@@ -0,0 +1,92 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <array>
#include <vector>
#include <cassert>
//[ filtered_int_iterator_defn
template<typename Pred>
struct filtered_int_iterator : boost::stl_interfaces::iterator_interface<
filtered_int_iterator<Pred>,
std::bidirectional_iterator_tag,
int>
{
filtered_int_iterator() : it_(nullptr) {}
filtered_int_iterator(int * it, int * last, Pred pred) :
it_(it),
last_(last),
pred_(std::move(pred))
{
// We need to do this in the constructor so that operator== works
// properly on two filtered_int_iterators, when they bound a sequence
// in which none of the ints meets the predicate.
it_ = std::find_if(it_, last_, pred_);
}
// A bidirectional iterator based on iterator_interface usually required
// four user-defined operations. since we are adapting an existing
// iterator (an int *), we only need to define this one. The others are
// implemented by iterator_interface, using the underlying int *.
filtered_int_iterator & operator++()
{
it_ = std::find_if(std::next(it_), last_, pred_);
return *this;
}
// It is really common for iterator adaptors to have a base() member
// function that returns the adapted iterator.
int * base() const { return it_; }
private:
// Provide access to these private members.
friend boost::stl_interfaces::access;
// These functions are picked up by iterator_interface, and used to
// implement any operations that you don't define above. They're not
// called base() so that they do not collide with the base() member above.
//
// Note that the const overload does not strictly speaking need to be a
// reference, as demonstrated here.
constexpr int *& base_reference() noexcept { return it_; }
constexpr int * base_reference() const noexcept { return it_; }
int * it_;
int * last_;
Pred pred_;
};
// A make-function makes it easier to deal with the Pred parameter.
template<typename Pred>
auto make_filtered_int_iterator(int * it, int * last, Pred pred)
{
return filtered_int_iterator<Pred>(it, last, std::move(pred));
}
//]
int main()
{
//[ filtered_int_iterator_usage
std::array<int, 8> ints = {{0, 1, 2, 3, 4, 5, 6, 7}};
int * const ints_first = ints.data();
int * const ints_last = ints.data() + ints.size();
auto even = [](int x) { return (x % 2) == 0; };
auto first = make_filtered_int_iterator(ints_first, ints_last, even);
auto last = make_filtered_int_iterator(ints_last, ints_last, even);
// This is an example only. Obviously, we could have called
// std::copy_if() here.
std::vector<int> ints_copy;
std::copy(first, last, std::back_inserter(ints_copy));
assert(ints_copy == (std::vector<int>{0, 2, 4, 6}));
//]
}

View File

@@ -0,0 +1,87 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
//[ interoperability
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <array>
#include <numeric>
#include <cassert>
// This is a random access iterator templated on a value type. The ValueType
// template parameter allows us easily to define const and non-const iterators
// from the same template.
template<typename ValueType>
struct random_access_iterator : boost::stl_interfaces::iterator_interface<
random_access_iterator<ValueType>,
std::random_access_iterator_tag,
ValueType>
{
static_assert(std::is_object<ValueType>::value, "");
// Default constructor.
constexpr random_access_iterator() noexcept {}
// Construction from an underlying pointer.
constexpr random_access_iterator(ValueType * it) noexcept : it_(it) {}
// Implicit conversion from an existing random_access_iterator with a
// possibly different value type. The enable_if logic here just enforces
// that this constructor only participates in overload resolution when the
// expression it_ = other.it_ is well-formed.
template<
typename ValueType2,
typename E = std::enable_if_t<
std::is_convertible<ValueType2 *, ValueType *>::value>>
constexpr random_access_iterator(
random_access_iterator<ValueType2> other) noexcept :
it_(other.it_)
{}
constexpr ValueType & operator*() const noexcept { return *it_; }
constexpr random_access_iterator & operator+=(std::ptrdiff_t i) noexcept
{
it_ += i;
return *this;
}
constexpr auto operator-(random_access_iterator other) const noexcept
{
return it_ - other.it_;
}
private:
ValueType * it_;
// This friendship is necessary to enable the implicit conversion
// constructor above to work.
template<typename ValueType2>
friend struct random_access_iterator;
};
using iterator = random_access_iterator<int>;
using const_iterator = random_access_iterator<int const>;
int main()
{
std::array<int, 10> ints = {{0, 2, 1, 3, 4, 5, 7, 6, 8, 9}};
// Create and use two mutable iterators.
iterator first(ints.data());
iterator last(ints.data() + ints.size());
std::sort(first, last);
assert(ints == (std::array<int, 10>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}));
// Create and use two constant iterators, one from an existing mutable
// iterator.
std::array<int, 10> int_sums;
const_iterator cfirst(ints.data());
const_iterator clast = last;
std::partial_sum(cfirst, clast, int_sums.begin());
assert(int_sums == (std::array<int, 10>{{0, 1, 3, 6, 10, 15, 21, 28, 36, 45}}));
}
//]

View File

@@ -0,0 +1,89 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <array>
#include <iostream>
#include <cassert>
//[ node_defn
template<typename T>
struct node
{
T value_;
node * next_; // == nullptr in the tail node
};
//]
//[ node_iterator_class_head
template<typename T>
struct node_iterator
: boost::stl_interfaces::
iterator_interface<node_iterator<T>, std::forward_iterator_tag, T>
//]
{
//[ node_iterator_ctors
constexpr node_iterator() noexcept : it_(nullptr) {}
constexpr node_iterator(node<T> * it) noexcept : it_(it) {}
//]
//[ node_iterator_user_ops
constexpr T & operator*() const noexcept { return it_->value_; }
constexpr node_iterator & operator++() noexcept
{
it_ = it_->next_;
return *this;
}
friend constexpr bool
operator==(node_iterator lhs, node_iterator rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
//]
//[ node_iterator_using_declaration
using base_type = boost::stl_interfaces::
iterator_interface<node_iterator<T>, std::forward_iterator_tag, T>;
using base_type::operator++;
//]
private:
node<T> * it_;
};
//[ node_iterator_concept_check Equivalent to
// static_assert(std::forward_iterator<node_iterator>, ""), or nothing in
// C++17 and earlier.
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(node_iterator, std::forward_iterator)
//]
int main()
{
std::array<node<int>, 5> nodes;
std::generate(nodes.begin(), nodes.end(), [] {
static int i = 0;
return node<int>{i++};
});
std::adjacent_find(
nodes.begin(), nodes.end(), [](node<int> & a, node<int> & b) {
a.next_ = &b;
return false;
});
nodes.back().next_ = nullptr;
//[ node_iterator_usage
node_iterator<int> const first(&nodes[0]);
node_iterator<int> const last;
for (auto it = first; it != last; it++) {
std::cout << *it << " "; // Prints 0 1 2 3 4
}
std::cout << "\n";
//]
}

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
//[ random_access_iterator
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <array>
#include <cassert>
// This is a minimal random access iterator. It uses default template
// parameters for most stl_interfaces template parameters.
struct simple_random_access_iterator
: boost::stl_interfaces::iterator_interface<
simple_random_access_iterator,
std::random_access_iterator_tag,
int>
{
// This default constructor does not initialize it_, since that's how int *
// works as well. This allows optimum performance in code paths where
// initializing a single pointer may be measurable. It is also a
// reasonable choice to initialize with nullptr.
simple_random_access_iterator() noexcept {}
simple_random_access_iterator(int * it) noexcept : it_(it) {}
int & operator*() const noexcept { return *it_; }
simple_random_access_iterator & operator+=(std::ptrdiff_t i) noexcept
{
it_ += i;
return *this;
}
auto operator-(simple_random_access_iterator other) const noexcept
{
return it_ - other.it_;
}
private:
int * it_;
};
int main()
{
std::array<int, 10> ints = {{0, 2, 1, 3, 4, 5, 7, 6, 8, 9}};
simple_random_access_iterator first(ints.data());
simple_random_access_iterator last(ints.data() + ints.size());
std::sort(first, last);
assert(ints == (std::array<int, 10>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}));
}
//]

View File

@@ -0,0 +1,60 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <string>
#include <cassert>
//[ repeated_chars_iterator
struct repeated_chars_iterator : boost::stl_interfaces::iterator_interface<
repeated_chars_iterator,
std::random_access_iterator_tag,
char,
char>
{
constexpr repeated_chars_iterator() noexcept :
first_(nullptr),
size_(0),
n_(0)
{}
constexpr repeated_chars_iterator(
char const * first, difference_type size, difference_type n) noexcept :
first_(first),
size_(size),
n_(n)
{}
constexpr char operator*() const noexcept { return first_[n_ % size_]; }
constexpr repeated_chars_iterator & operator+=(std::ptrdiff_t i) noexcept
{
n_ += i;
return *this;
}
constexpr auto operator-(repeated_chars_iterator other) const noexcept
{
return n_ - other.n_;
}
private:
char const * first_;
difference_type size_;
difference_type n_;
};
//]
int main()
{
//[ repeated_chars_iterator_usage
repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position.
repeated_chars_iterator last("foo", 3, 7); // Same as above, but now the iterator's position is 7.
std::string result;
std::copy(first, last, std::back_inserter(result));
assert(result == "foofoof");
//]
}

View File

@@ -0,0 +1,124 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
//[ reverse_iterator
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <list>
#include <vector>
#include <cassert>
// In all the previous examples, we only had to implement a subset of the six
// possible user-defined basis operations that was needed for one particular
// iterator concept. For reverse_iterator, we want to support bidirectional,
// random access, and contiguous iterators. We therefore need to provide all
// the basis operations that might be needed.
template<typename BidiIter>
struct reverse_iterator
: boost::stl_interfaces::iterator_interface<
reverse_iterator<BidiIter>,
#if 201703L < __cplusplus && defined(__cpp_lib_ranges)
boost::stl_interfaces::v2::detail::iter_concept_t<BidiIter>,
#else
typename std::iterator_traits<BidiIter>::iterator_category,
#endif
typename std::iterator_traits<BidiIter>::value_type>
{
reverse_iterator() : it_() {}
reverse_iterator(BidiIter it) : it_(it) {}
using ref_t = typename std::iterator_traits<BidiIter>::reference;
using diff_t = typename std::iterator_traits<BidiIter>::difference_type;
ref_t operator*() const { return *std::prev(it_); }
// These three are used only when BidiIter::iterator_category is
// std::bidirectional_iterator_tag.
bool operator==(reverse_iterator other) const { return it_ == other.it_; }
// Even though iterator_interface-derived bidirectional iterators are
// usually given operator++() and operator--() members, it turns out that
// operator+=() below amounts to the same thing. That's good, since
// having operator++() and operator+=() in this class would have lead to
// ambiguities in iterator_interface.
// These two are only used when BidiIter::iterator_category is
// std::random_access_iterator_tag or std::contiguous_iterator_tag. Even
// so, they need to compile even when BidiIter::iterator_category is
// std::bidirectional_iterator_tag. That means we have to use
// std::distance() and std::advance() instead of operator-() and
// operator+=().
//
// Don't worry, the O(n) bidirectional implementations of std::distance()
// and std::advance() are dead code, because compare() and advance() are
// never even called when BidiIter::iterator_category is
// std::bidirectional_iterator_tag.
diff_t operator-(reverse_iterator other) const
{
return -std::distance(other.it_, it_);
}
reverse_iterator & operator+=(diff_t n)
{
std::advance(it_, -n);
return *this;
}
// No need for a using declaration to make
// iterator_interface::operator++(int) visible, because we're not defining
// operator++() in this template.
private:
BidiIter it_;
};
using rev_bidi_iter = reverse_iterator<std::list<int>::iterator>;
using rev_ra_iter = reverse_iterator<std::vector<int>::iterator>;
int main()
{
{
std::list<int> ints = {4, 3, 2};
std::list<int> ints_copy;
std::copy(
rev_bidi_iter(ints.end()),
rev_bidi_iter(ints.begin()),
std::back_inserter(ints_copy));
std::reverse(ints.begin(), ints.end());
assert(ints_copy == ints);
}
{
std::vector<int> ints = {4, 3, 2};
std::vector<int> ints_copy(ints.size());
std::copy(
rev_ra_iter(ints.end()),
rev_ra_iter(ints.begin()),
ints_copy.begin());
std::reverse(ints.begin(), ints.end());
assert(ints_copy == ints);
}
{
using rev_ptr_iter = reverse_iterator<int *>;
int ints[3] = {4, 3, 2};
int ints_copy[3];
std::copy(
rev_ptr_iter(std::end(ints)),
rev_ptr_iter(std::begin(ints)),
std::begin(ints_copy));
std::reverse(std::begin(ints), std::end(ints));
assert(std::equal(
std::begin(ints_copy),
std::end(ints_copy),
std::begin(ints),
std::end(ints)));
}
}
//]

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include "static_vector.hpp"
int main()
{
//[ static_vector_usage
static_vector<int, 128> vec;
vec.push_back(42);
vec.push_back(13);
assert(vec[0] == 42);
assert(vec[1] == 13);
assert(vec.size() == 2u);
vec.erase(vec.end() - 1);
static_vector<int, 128> tmp({42});
assert(vec == (static_vector<int, 128>({42})));
//]
}

View File

@@ -0,0 +1,283 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/sequence_container_interface.hpp>
#include <algorithm>
#include <iterator>
#include <memory>
#include <tuple>
#include <cassert>
// There's a test that uses this example header; this controls whether the
// C++14 version (v1::) of sequence_container_interface gets used, or the
// C++20 version (v2::).
#if defined(USE_V2)
template<typename Derived, boost::stl_interfaces::element_layout Contiguity>
using sequence_container_interface =
boost::stl_interfaces::v2::sequence_container_interface<Derived>;
#else
template<typename Derived, boost::stl_interfaces::element_layout Contiguity>
using sequence_container_interface =
boost::stl_interfaces::v1::sequence_container_interface<Derived, Contiguity>;
#endif
//[ static_vector_defn
// The sections of member functions below are commented as they are in the
// standard for std::vector. Each section has two numbers: the number of
// member functions in that section, and the number that are missing, because
// they are provided by sequence_container_interface. The purely
// allocator-specific members are neither present nor part of the counts.
//
// We're passing boost::stl_interfaces::contiguous here, so that
// sequence_container_interface knows that it should provide data().
template<typename T, std::size_t N>
struct static_vector : sequence_container_interface<
static_vector<T, N>,
boost::stl_interfaces::element_layout::contiguous>
{
// These are the types required for reversible containers. These must be
// user-defined.
using value_type = T;
using pointer = T *;
using const_pointer = T const *;
using reference = value_type &;
using const_reference = value_type const &;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using iterator = T *;
using const_iterator = T const *;
using reverse_iterator = boost::stl_interfaces::reverse_iterator<iterator>;
using const_reverse_iterator =
boost::stl_interfaces::reverse_iterator<const_iterator>;
// construct/copy/destroy (9 members, skipped 2)
//
// Constructors and special member functions all must be user-provided.
// Were they provided by sequence_container_interface, everything would break, due
// to the language rules related to them. However, assignment from
// std::initializer_list and the destructor can come from
// sequence_container_interface.
static_vector() noexcept : size_(0) {}
explicit static_vector(size_type n) : size_(0) { resize(n); }
explicit static_vector(size_type n, T const & x) : size_(0)
{
// Note that you must write "this->" before all the member functions
// provided by sequence_container_interface, which is slightly annoying.
this->assign(n, x);
}
template<
typename InputIterator,
typename Enable = std::enable_if_t<std::is_convertible<
typename std::iterator_traits<InputIterator>::iterator_category,
std::input_iterator_tag>::value>>
static_vector(InputIterator first, InputIterator last) : size_(0)
{
this->assign(first, last);
}
static_vector(std::initializer_list<T> il) :
static_vector(il.begin(), il.end())
{}
static_vector(static_vector const & other) : size_(0)
{
this->assign(other.begin(), other.end());
}
static_vector(static_vector && other) noexcept(
noexcept(std::declval<static_vector>().emplace_back(
std::move(*other.begin())))) :
size_(0)
{
for (auto & element : other) {
emplace_back(std::move(element));
}
other.clear();
}
static_vector & operator=(static_vector const & other)
{
this->clear();
this->assign(other.begin(), other.end());
return *this;
}
static_vector & operator=(static_vector && other) noexcept(noexcept(
std::declval<static_vector>().emplace_back(std::move(*other.begin()))))
{
this->clear();
for (auto & element : other) {
emplace_back(std::move(element));
}
other.clear();
return *this;
}
~static_vector() { this->clear(); }
// iterators (2 members, skipped 10)
//
// This section is the first big win. Instead of having to write 12
// overloads line begin, cbegin, rbegin, crbegin, etc., we can just write
// 2.
iterator begin() noexcept { return reinterpret_cast<T *>(buf_); }
iterator end() noexcept
{
return reinterpret_cast<T *>(buf_ + size_ * sizeof(T));
}
// capacity (6 members, skipped 2)
//
// Most of these are not even part of the general requirements, because
// some are specific to std::vector and related types. However, we do get
// empty and size from sequence_container_interface.
size_type max_size() const noexcept { return N; }
size_type capacity() const noexcept { return N; }
void resize(size_type sz) noexcept
{
resize_impl(sz, [] { return T(); });
}
void resize(size_type sz, T const & x) noexcept
{
resize_impl(sz, [&]() -> T const & { return x; });
}
void reserve(size_type n) noexcept { assert(n < capacity()); }
void shrink_to_fit() noexcept {}
// element access (skipped 8)
// data access (skipped 2)
//
// Another big win. sequence_container_interface provides all of the
// overloads of operator[], at, front, back, and data.
// modifiers (5 members, skipped 9)
//
// In this section we again get most of the API from
// sequence_container_interface.
// emplace_back does not look very necessary -- just look at its trivial
// implementation -- but we can't provide it from
// sequence_container_interface, because it is an optional sequence
// container interface. We would not want emplace_front to suddenly
// appear on our std::vector-like type, and there may be some other type
// for which emplace_back is a bad idea.
//
// However, by providing emplace_back here, we signal to the
// sequence_container_interface template that our container is
// back-mutation-friendly, and this allows it to provide all the overloads
// of push_back and pop_back.
template<typename... Args>
reference emplace_back(Args &&... args)
{
return *emplace(end(), std::forward<Args>(args)...);
}
template<typename... Args>
iterator emplace(const_iterator pos, Args &&... args)
{
auto position = const_cast<T *>(pos);
bool const insert_before_end = position < end();
if (insert_before_end) {
auto last = end();
emplace_back(std::move(this->back()));
std::move_backward(position, last - 1, last);
}
new (position) T(std::forward<Args>(args)...);
if (!insert_before_end)
++size_;
return position;
}
// Note: The iterator category here was upgraded to ForwardIterator
// (instead of vector's InputIterator), to ensure linear time complexity.
template<
typename ForwardIterator,
typename Enable = std::enable_if_t<std::is_convertible<
typename std::iterator_traits<ForwardIterator>::iterator_category,
std::forward_iterator_tag>::value>>
iterator
insert(const_iterator pos, ForwardIterator first, ForwardIterator last)
{
auto position = const_cast<T *>(pos);
auto const insertions = std::distance(first, last);
assert(this->size() + insertions < capacity());
uninitialized_generate(end(), end() + insertions, [] { return T(); });
std::move_backward(position, end(), end() + insertions);
std::copy(first, last, position);
size_ += insertions;
return position;
}
iterator erase(const_iterator f, const_iterator l)
{
auto first = const_cast<T *>(f);
auto last = const_cast<T *>(l);
auto end_ = this->end();
auto it = std::move(last, end_, first);
for (; it != end_; ++it) {
it->~T();
}
size_ -= last - first;
return first;
}
void swap(static_vector & other)
{
size_type short_size, long_size;
std::tie(short_size, long_size) =
std::minmax(this->size(), other.size());
for (auto i = size_type(0); i < short_size; ++i) {
using std::swap;
swap((*this)[i], other[i]);
}
static_vector * longer = this;
static_vector * shorter = this;
if (this->size() < other.size())
longer = &other;
else
shorter = &other;
for (auto it = longer->begin() + short_size, last = longer->end();
it != last;
++it) {
shorter->emplace_back(std::move(*it));
}
longer->resize(short_size);
shorter->size_ = long_size;
}
// Since we're getting so many overloads from
// sequence_container_interface, and since many of those overloads are
// implemented in terms of a user-defined function of the same name, we
// need to add quite a few using declarations here.
using base_type = sequence_container_interface<
static_vector<T, N>,
boost::stl_interfaces::element_layout::contiguous>;
using base_type::begin;
using base_type::end;
using base_type::insert;
using base_type::erase;
// comparisons (skipped 6)
private:
template<typename F>
static void uninitialized_generate(iterator f, iterator l, F func)
{
for (; f != l; ++f) {
new (static_cast<void *>(std::addressof(*f))) T(func());
}
}
template<typename F>
void resize_impl(size_type sz, F func) noexcept
{
assert(sz < capacity());
if (sz < this->size())
erase(begin() + sz, end());
if (this->size() < sz)
uninitialized_generate(end(), begin() + sz, func);
size_ = sz;
}
alignas(T) unsigned char buf_[N * sizeof(T)];
size_type size_;
};
//]

View File

@@ -0,0 +1,119 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
//[ zip_proxy_iterator
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <algorithm>
#include <array>
#include <tuple>
#include <cassert>
// This is a zip iterator, meaning that it iterates over a notional sequence
// of pairs that is formed from two actual sequences of scalars. To make this
// iterator writable, it needs to have a reference type that is not actually a
// reference -- the reference type is a pair of references, std::tuple<int &,
// int &>.
struct zip_iterator : boost::stl_interfaces::proxy_iterator_interface<
zip_iterator,
std::random_access_iterator_tag,
std::tuple<int, int>,
std::tuple<int &, int &>>
{
constexpr zip_iterator() noexcept : it1_(), it2_() {}
constexpr zip_iterator(int * it1, int * it2) noexcept : it1_(it1), it2_(it2)
{}
constexpr std::tuple<int &, int &> operator*() const noexcept
{
return std::tuple<int &, int &>{*it1_, *it2_};
}
constexpr zip_iterator & operator += (std::ptrdiff_t i) noexcept
{
it1_ += i;
it2_ += i;
return *this;
}
constexpr auto operator-(zip_iterator other) const noexcept
{
return it1_ - other.it1_;
}
private:
int * it1_;
int * it2_;
};
namespace std {
// Required for std::sort to work with zip_iterator. Without this
// overload, std::sort eventually tries to call std::swap(*it1, *it2),
// *it1 and *it2 are rvalues, and std::swap() takes mutable lvalue
// references. That makes std::swap(*it1, *it2) ill-formed.
//
// Note that this overload does not conflict with any other swap()
// overloads, since this one takes rvalue reference parameters.
//
// Also note that this overload has to be in namespace std only because
// ADL cannot find it anywhere else. If
// zip_iterator::reference/std::tuple's template parameters were not
// builtins, this overload could be in whatever namespace those template
// parameters were declared in.
void swap(zip_iterator::reference && lhs, zip_iterator::reference && rhs)
{
using std::swap;
swap(std::get<0>(lhs), std::get<0>(rhs));
swap(std::get<1>(lhs), std::get<1>(rhs));
}
}
int main()
{
std::array<int, 10> ints = {{2, 0, 1, 5, 3, 6, 8, 4, 9, 7}};
std::array<int, 10> ones = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
{
std::array<std::tuple<int, int>, 10> const result = {{
{2, 1},
{0, 1},
{1, 1},
{5, 1},
{3, 1},
{6, 1},
{8, 1},
{4, 1},
{9, 1},
{7, 1},
}};
zip_iterator first(ints.data(), ones.data());
zip_iterator last(ints.data() + ints.size(), ones.data() + ones.size());
assert(std::equal(first, last, result.begin(), result.end()));
}
{
std::array<std::tuple<int, int>, 10> const result = {{
{0, 1},
{1, 1},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{6, 1},
{7, 1},
{8, 1},
{9, 1},
}};
zip_iterator first(ints.data(), ones.data());
zip_iterator last(ints.data() + ints.size(), ones.data() + ones.size());
assert(!std::equal(first, last, result.begin(), result.end()));
std::sort(first, last);
assert(std::equal(first, last, result.begin(), result.end()));
}
}
//]

View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; URL=../../doc/html/stl_interfaces.html">
</head>
<body>
Automatic redirection failed, click this <a href="../../doc/html/stl_interfaces.html">link</a>
<hr>
<p>Copyright T. Zachary Laine 2019</p>
<p>
Distributed under the Boost Software License, Version 1.0.
(See accompanying file <a href="LICENSE.md">LICENSE.md</a> or copy at
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)
</p>
</body>
</html>

View File

@@ -0,0 +1,8 @@
{
"key": "stl_interfaces",
"name": "stl_interfaces",
"authors": [ "T. Zachary Laine" ],
"maintainers": [ "Zach Laine <whatwasthataddress -at- gmail.com>" ],
"description": "C++14 and later CRTP templates for defining iterators, views, and containers.",
"category": [ "Generic" ]
}

View File

@@ -0,0 +1,45 @@
# Copyright (C) 2019 T. Zachary Laine
#
# 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)
include_directories(${CMAKE_HOME_DIRECTORY})
include(CTest)
enable_testing()
add_custom_target(stl_interfaces_check COMMAND ${CMAKE_CTEST_COMMAND} -j4 -C ${CMAKE_CFG_INTDIR})
if (NOT TARGET check)
add_custom_target(check DEPENDS stl_interfaces_check)
else()
add_dependencies(check stl_interfaces_check)
endif()
set(warnings_flag)
if (NOT MSVC)
set(warnings_flag -Wall)
endif ()
macro(add_test_executable name)
add_executable(${name} ${name}.cpp)
target_compile_options(${name} PRIVATE ${warnings_flag})
target_link_libraries(${name} stl_interfaces)
target_compile_definitions(${name} PRIVATE BOOST_NO_AUTO_PTR)
set_property(TARGET ${name} PROPERTY CXX_STANDARD ${CXX_STD})
add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name})
if (clang_on_linux)
target_link_libraries(${name} c++)
endif ()
endmacro()
add_test_executable(input)
add_test_executable(output)
add_test_executable(forward)
add_test_executable(bidirectional)
add_test_executable(random_access)
add_test_executable(reverse_iter)
add_test_executable(detail)
add_test_executable(static_vec)
add_test_executable(static_vec_noncopyable)
add_test_executable(array)

View File

@@ -0,0 +1,26 @@
# Boost.STLInterfaces Library Tests Jamfile
# Copyright Oliver Kowalke 2013.
# Copyright Modified Work Barrett Adair 2020
# 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)
import testing ;
import ../../config/checks/config : requires ;
project :
requirements
[ requires cxx14_constexpr ]
;
run forward.cpp ;
run detail.cpp ;
run array.cpp ;
run output.cpp ;
run input.cpp ;
run reverse_iter.cpp ;
run static_vec_noncopyable.cpp ;
run bidirectional.cpp ;
run random_access.cpp ;
run static_vec.cpp ;

View File

@@ -0,0 +1,589 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/sequence_container_interface.hpp>
#include "ill_formed.hpp"
#include <boost/core/lightweight_test.hpp>
#include <array>
#include <deque>
#include <vector>
// Just like std::array, except for the 0-size specialization, and the fact
// that the base class makes brace-initialization wonky.
template<typename T, std::size_t N>
struct array : boost::stl_interfaces::sequence_container_interface<
array<T, N>,
boost::stl_interfaces::v1::element_layout::contiguous>
{
using value_type = T;
using pointer = T *;
using const_pointer = T const *;
using reference = value_type &;
using const_reference = value_type const &;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using iterator = T *;
using const_iterator = T const *;
using reverse_iterator = boost::stl_interfaces::reverse_iterator<iterator>;
using const_reverse_iterator =
boost::stl_interfaces::reverse_iterator<const_iterator>;
void fill(T const & x) { std::fill(begin(), end(), x); }
iterator begin() noexcept { return elements_; }
iterator end() noexcept { return elements_ + N; }
size_type max_size() const noexcept { return N; }
void swap(array & other)
{
using std::swap;
T * element = elements_;
for (auto & x : other) {
swap(*element++, x);
}
}
using base_type = boost::stl_interfaces::sequence_container_interface<
array<T, N>,
boost::stl_interfaces::v1::element_layout::contiguous>;
using base_type::begin;
using base_type::end;
T elements_[N];
};
using arr_type = array<int, 5>;
void test_comparisons()
{
arr_type sm;
sm[0] = 1;
sm[1] = 2;
sm[2] = 3;
sm[3] = 0;
sm[4] = 0;
arr_type md;
md[0] = 1;
md[1] = 2;
md[2] = 3;
md[3] = 4;
md[4] = 0;
arr_type lg;
lg[0] = 1;
lg[1] = 2;
lg[2] = 3;
lg[3] = 4;
lg[4] = 5;
BOOST_TEST(sm == sm);
BOOST_TEST(!(sm == md));
BOOST_TEST(!(sm == lg));
BOOST_TEST(!(sm != sm));
BOOST_TEST(sm != md);
BOOST_TEST(sm != lg);
BOOST_TEST(!(sm < sm));
BOOST_TEST(sm < md);
BOOST_TEST(sm < lg);
BOOST_TEST(sm <= sm);
BOOST_TEST(sm <= md);
BOOST_TEST(sm <= lg);
BOOST_TEST(!(sm > sm));
BOOST_TEST(!(sm > md));
BOOST_TEST(!(sm > lg));
BOOST_TEST(sm >= sm);
BOOST_TEST(!(sm >= md));
BOOST_TEST(!(sm >= lg));
BOOST_TEST(!(md == sm));
BOOST_TEST(md == md);
BOOST_TEST(!(md == lg));
BOOST_TEST(!(md < sm));
BOOST_TEST(!(md < md));
BOOST_TEST(md < lg);
BOOST_TEST(!(md <= sm));
BOOST_TEST(md <= md);
BOOST_TEST(md <= lg);
BOOST_TEST(md > sm);
BOOST_TEST(!(md > md));
BOOST_TEST(!(md > lg));
BOOST_TEST(md >= sm);
BOOST_TEST(md >= md);
BOOST_TEST(!(md >= lg));
BOOST_TEST(!(lg == sm));
BOOST_TEST(!(lg == md));
BOOST_TEST(lg == lg);
BOOST_TEST(!(lg < sm));
BOOST_TEST(!(lg < md));
BOOST_TEST(!(lg < lg));
BOOST_TEST(!(lg <= sm));
BOOST_TEST(!(lg <= md));
BOOST_TEST(lg <= lg);
BOOST_TEST(lg > sm);
BOOST_TEST(lg > md);
BOOST_TEST(!(lg > lg));
BOOST_TEST(lg >= sm);
BOOST_TEST(lg >= md);
BOOST_TEST(lg >= lg);
}
void test_swap()
{
{
arr_type v1;
v1[0] = 3;
v1[1] = 4;
v1[2] = 0;
v1[3] = 0;
v1[4] = 0;
arr_type v2;
v2[0] = 4;
v2[1] = 3;
v2[2] = 0;
v2[3] = 0;
v2[4] = 0;
arr_type const v1_copy = v1;
arr_type const v2_copy = v2;
static_assert(std::is_same<decltype(v1.swap(v2)), void>::value, "");
static_assert(std::is_same<decltype(swap(v1, v2)), void>::value, "");
v1.swap(v2);
BOOST_TEST(v1 == v2_copy);
BOOST_TEST(v2 == v1_copy);
}
{
arr_type v1;
v1[0] = 3;
v1[1] = 4;
v1[2] = 0;
v1[3] = 0;
v1[4] = 0;
arr_type v2;
v2[0] = 4;
v2[1] = 3;
v2[2] = 0;
v2[3] = 0;
v2[4] = 0;
arr_type const v1_copy = v1;
arr_type const v2_copy = v2;
swap(v1, v2);
BOOST_TEST(v1 == v2_copy);
BOOST_TEST(v2 == v1_copy);
}
}
template<typename Iter>
using writable_iter_t = decltype(
*std::declval<Iter>() =
std::declval<typename std::iterator_traits<Iter>::value_type>());
static_assert(
!ill_formed<writable_iter_t, decltype(std::declval<arr_type &>().begin())>::
value,
"");
static_assert(
!ill_formed<writable_iter_t, decltype(std::declval<arr_type &>().end())>::
value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<arr_type const &>().begin())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<arr_type const &>().end())>::value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<arr_type &>().cbegin())>::
value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<arr_type &>().cend())>::
value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<arr_type const &>().rbegin())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<arr_type const &>().rend())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<arr_type &>().crbegin())>::value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<arr_type &>().crend())>::
value,
"");
void test_iterators()
{
arr_type v0;
v0[0] = 3;
v0[1] = 2;
v0[2] = 1;
v0[3] = 0;
v0[4] = 0;
{
arr_type v = v0;
static_assert(
std::is_same<decltype(v.begin()), arr_type::iterator>::value, "");
static_assert(
std::is_same<decltype(v.end()), arr_type::iterator>::value, "");
static_assert(
std::is_same<decltype(v.cbegin()), arr_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cend()), arr_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.rbegin()), arr_type::reverse_iterator>::
value,
"");
static_assert(
std::is_same<decltype(v.rbegin()), arr_type::reverse_iterator>::
value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
arr_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
arr_type::const_reverse_iterator>::value,
"");
std::array<int, 5> const a = {{3, 2, 1, 0, 0}};
std::array<int, 5> const ra = {{0, 0, 1, 2, 3}};
BOOST_TEST(std::equal(v.begin(), v.end(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.cbegin(), v.cend(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.rbegin(), v.rend(), ra.begin(), ra.end()));
BOOST_TEST(std::equal(v.crbegin(), v.crend(), ra.begin(), ra.end()));
arr_type v2;
v2[0] = 8;
v2[1] = 2;
v2[2] = 1;
v2[3] = 0;
v2[4] = 9;
*v.begin() = 8;
*v.rbegin() = 9;
BOOST_TEST(v == v2);
}
{
arr_type const v = v0;
static_assert(
std::is_same<decltype(v.begin()), arr_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.end()), arr_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cbegin()), arr_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cend()), arr_type::const_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.rbegin()),
arr_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.rbegin()),
arr_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
arr_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
arr_type::const_reverse_iterator>::value,
"");
std::array<int, 5> const a = {{3, 2, 1, 0, 0}};
std::array<int, 5> const ra = {{0, 0, 1, 2, 3}};
BOOST_TEST(std::equal(v.begin(), v.end(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.cbegin(), v.cend(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.rbegin(), v.rend(), ra.begin(), ra.end()));
BOOST_TEST(std::equal(v.crbegin(), v.crend(), ra.begin(), ra.end()));
}
}
template<
typename Container,
typename ValueType = typename Container::value_type>
using lvalue_push_front_t = decltype(
std::declval<Container &>().push_front(std::declval<ValueType const &>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using rvalue_push_front_t = decltype(std::declval<Container &>().push_front(0));
template<typename Container>
using pop_front_t = decltype(std::declval<Container &>().pop_front());
static_assert(ill_formed<lvalue_push_front_t, arr_type>::value, "");
static_assert(ill_formed<rvalue_push_front_t, arr_type>::value, "");
static_assert(ill_formed<pop_front_t, arr_type>::value, "");
using std_deq_int = std::deque<int>;
static_assert(!ill_formed<lvalue_push_front_t, std_deq_int>::value, "");
static_assert(!ill_formed<rvalue_push_front_t, std_deq_int>::value, "");
static_assert(!ill_formed<pop_front_t, std_deq_int>::value, "");
template<
typename Container,
typename ValueType = typename Container::value_type>
using lvalue_push_back_t = decltype(
std::declval<Container &>().push_back(std::declval<ValueType const &>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using rvalue_push_back_t = decltype(std::declval<Container &>().push_back(0));
template<typename Container>
using pop_back_t = decltype(std::declval<Container &>().pop_back());
static_assert(ill_formed<lvalue_push_back_t, arr_type>::value, "");
static_assert(ill_formed<rvalue_push_back_t, arr_type>::value, "");
static_assert(ill_formed<pop_back_t, arr_type>::value, "");
using std_vec_int = std::vector<int>;
static_assert(!ill_formed<lvalue_push_back_t, std_vec_int>::value, "");
static_assert(!ill_formed<rvalue_push_back_t, std_vec_int>::value, "");
static_assert(!ill_formed<pop_back_t, std_vec_int>::value, "");
void test_front_back()
{
{
arr_type v;
v[0] = 0;
v[1] = 0;
v[2] = 0;
v[3] = 0;
v[4] = 0;
static_assert(std::is_same<decltype(v.front()), int &>::value, "");
static_assert(std::is_same<decltype(v.back()), int &>::value, "");
v.front() = 9;
v.back() = 8;
BOOST_TEST(v[0] == v.front());
BOOST_TEST(v[4] == v.back());
}
{
arr_type v0;
v0[0] = 3;
v0[1] = 0;
v0[2] = 2;
v0[3] = 0;
v0[4] = 1;
arr_type const v = v0;
BOOST_TEST(v.front() == 3);
BOOST_TEST(v.back() == 1);
static_assert(
std::is_same<decltype(v.front()), int const &>::value, "");
static_assert(std::is_same<decltype(v.back()), int const &>::value, "");
}
}
void test_cindex_at()
{
arr_type v0;
v0[0] = 3;
v0[1] = 2;
v0[2] = 1;
v0[3] = 0;
v0[4] = 0;
{
arr_type v = v0;
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
BOOST_TEST(v[2] == 1);
BOOST_TEST_NO_THROW(v.at(0));
BOOST_TEST_NO_THROW(v.at(1));
BOOST_TEST_NO_THROW(v.at(2));
BOOST_TEST_THROWS(v.at(5), std::out_of_range);
static_assert(std::is_same<decltype(v[0]), int &>::value, "");
static_assert(std::is_same<decltype(v.at(0)), int &>::value, "");
v[0] = 8;
v.at(1) = 9;
BOOST_TEST(v[0] == 8);
BOOST_TEST(v[1] == 9);
}
{
arr_type const v = v0;
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
BOOST_TEST(v[2] == 1);
BOOST_TEST_NO_THROW(v.at(0));
BOOST_TEST_NO_THROW(v.at(1));
BOOST_TEST_NO_THROW(v.at(2));
BOOST_TEST_THROWS(v.at(5), std::out_of_range);
static_assert(std::is_same<decltype(v[0]), int const &>::value, "");
static_assert(std::is_same<decltype(v.at(0)), int const &>::value, "");
}
}
template<typename Container>
using resize_t = decltype(std::declval<Container &>().resize(0));
static_assert(ill_formed<resize_t, arr_type>::value, "");
static_assert(!ill_formed<resize_t, std_vec_int>::value, "");
template<
typename Container,
typename ValueType = typename Container::value_type>
using lvalue_insert_t = decltype(std::declval<Container &>().insert(
std::declval<Container &>().begin(), std::declval<ValueType const &>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using rvalue_insert_t = decltype(std::declval<Container &>().insert(
std::declval<Container &>().begin(), std::declval<ValueType &&>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using insert_n_t = decltype(std::declval<Container &>().insert(
std::declval<Container &>().begin(), 2, std::declval<ValueType const &>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using insert_il_t = decltype(std::declval<Container &>().insert(
std::declval<Container &>().begin(), std::initializer_list<int>{}));
static_assert(ill_formed<lvalue_insert_t, arr_type>::value, "");
static_assert(ill_formed<rvalue_insert_t, arr_type>::value, "");
// TODO: Broken in v1. Adding the proper constraint ICE's GCC 8, or
// infinitely recurses within the compiler for GCC and Clang, depending on the
// constraint technique used.
#if 0
static_assert(ill_formed<insert_n_t, arr_type>::value, "");
#endif
static_assert(ill_formed<insert_il_t, arr_type>::value, "");
static_assert(!ill_formed<lvalue_insert_t, std_vec_int>::value, "");
static_assert(!ill_formed<rvalue_insert_t, std_vec_int>::value, "");
static_assert(!ill_formed<insert_n_t, std_vec_int>::value, "");
static_assert(!ill_formed<insert_il_t, std_vec_int>::value, "");
template<typename Container>
using erase_t = decltype(
std::declval<Container &>().erase(std::declval<Container &>().begin()));
static_assert(ill_formed<erase_t, arr_type>::value, "");
static_assert(!ill_formed<erase_t, std_vec_int>::value, "");
template<typename Container>
using assign_t = decltype(std::declval<Container &>().assign(
std::declval<int *>(), std::declval<int *>()));
template<typename Container>
using assign_n_t = decltype(std::declval<Container &>().assign(5, 5));
template<typename Container>
using assign_il_t =
decltype(std::declval<Container &>().assign(std::initializer_list<int>{}));
template<typename Container>
using assignment_operator_il_t =
decltype(std::declval<Container &>() = std::initializer_list<int>{});
static_assert(ill_formed<assign_t, arr_type>::value, "");
static_assert(ill_formed<assign_n_t, arr_type>::value, "");
static_assert(ill_formed<assign_il_t, arr_type>::value, "");
static_assert(ill_formed<assignment_operator_il_t, arr_type>::value, "");
static_assert(!ill_formed<assign_t, std_vec_int>::value, "");
static_assert(!ill_formed<assign_n_t, std_vec_int>::value, "");
static_assert(!ill_formed<assign_il_t, std_vec_int>::value, "");
static_assert(!ill_formed<assignment_operator_il_t, std_vec_int>::value, "");
template<typename Container>
using clear_t = decltype(std::declval<Container &>().clear());
static_assert(ill_formed<clear_t, arr_type>::value, "");
static_assert(!ill_formed<clear_t, std_vec_int>::value, "");
int main()
{
test_comparisons();
test_swap();
test_iterators();
test_front_back();
test_cindex_at();
return boost::report_errors();
}

View File

@@ -0,0 +1,668 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include "ill_formed.hpp"
#include <boost/core/lightweight_test.hpp>
#include <algorithm>
#include <array>
#include <numeric>
#include <list>
#include <type_traits>
struct basic_bidirectional_iter : boost::stl_interfaces::iterator_interface<
basic_bidirectional_iter,
std::bidirectional_iterator_tag,
int>
{
basic_bidirectional_iter() : it_(nullptr) {}
basic_bidirectional_iter(int * it) : it_(it) {}
int & operator*() const { return *it_; }
basic_bidirectional_iter & operator++()
{
++it_;
return *this;
}
basic_bidirectional_iter & operator--()
{
--it_;
return *this;
}
friend bool operator==(
basic_bidirectional_iter lhs, basic_bidirectional_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::iterator_interface<
basic_bidirectional_iter,
std::bidirectional_iterator_tag,
int>;
using base_type::operator++;
using base_type::operator--;
private:
int * it_;
};
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
basic_bidirectional_iter, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
basic_bidirectional_iter,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
static_assert(
!boost::stl_interfaces::v1::v1_dtl::
plus_eq<basic_bidirectional_iter, std::ptrdiff_t>::value,
"");
struct basic_adapted_bidirectional_ptr_iter
: boost::stl_interfaces::iterator_interface<
basic_adapted_bidirectional_ptr_iter,
std::bidirectional_iterator_tag,
int>
{
basic_adapted_bidirectional_ptr_iter() : it_(nullptr) {}
basic_adapted_bidirectional_ptr_iter(int * it) : it_(it) {}
private:
friend boost::stl_interfaces::access;
int *& base_reference() noexcept { return it_; }
int * base_reference() const noexcept { return it_; }
int * it_;
};
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
basic_adapted_bidirectional_ptr_iter, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
basic_adapted_bidirectional_ptr_iter,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
struct basic_adapted_bidirectional_list_iter
: boost::stl_interfaces::iterator_interface<
basic_adapted_bidirectional_list_iter,
std::bidirectional_iterator_tag,
int>
{
basic_adapted_bidirectional_list_iter() : it_() {}
basic_adapted_bidirectional_list_iter(std::list<int>::iterator it) : it_(it)
{}
private:
friend boost::stl_interfaces::access;
std::list<int>::iterator & base_reference() noexcept { return it_; }
std::list<int>::iterator base_reference() const noexcept { return it_; }
std::list<int>::iterator it_;
};
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
basic_adapted_bidirectional_list_iter, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
basic_adapted_bidirectional_list_iter,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
template<typename ValueType>
struct adapted_bidirectional_ptr_iter
: boost::stl_interfaces::iterator_interface<
adapted_bidirectional_ptr_iter<ValueType>,
std::bidirectional_iterator_tag,
ValueType>
{
adapted_bidirectional_ptr_iter() : it_(nullptr) {}
adapted_bidirectional_ptr_iter(ValueType * it) : it_(it) {}
template<typename ValueType2>
adapted_bidirectional_ptr_iter(
adapted_bidirectional_ptr_iter<ValueType2> other) :
it_(other.it_)
{}
template<typename ValueType2>
friend struct adapted_bidirectional_ptr_iter;
private:
friend boost::stl_interfaces::access;
ValueType *& base_reference() noexcept { return it_; }
ValueType * base_reference() const noexcept { return it_; }
ValueType * it_;
};
static_assert(
!boost::stl_interfaces::v1_dtl::ra_iter<
adapted_bidirectional_ptr_iter<int>>::value,
"");
static_assert(
!boost::stl_interfaces::v1_dtl::ra_iter<
adapted_bidirectional_ptr_iter<int const>>::value,
"");
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
adapted_bidirectional_ptr_iter<int>, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
adapted_bidirectional_ptr_iter<int>,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
adapted_bidirectional_ptr_iter<int const>, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
adapted_bidirectional_ptr_iter<int const>,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int const &,
int const *,
std::ptrdiff_t)
template<typename ValueType>
struct bidirectional_iter : boost::stl_interfaces::iterator_interface<
bidirectional_iter<ValueType>,
std::bidirectional_iterator_tag,
ValueType>
{
bidirectional_iter() : it_(nullptr) {}
bidirectional_iter(ValueType * it) : it_(it) {}
template<
typename ValueType2,
typename E = std::enable_if_t<
std::is_convertible<ValueType2 *, ValueType *>::value>>
bidirectional_iter(bidirectional_iter<ValueType2> it) : it_(it.it_)
{}
ValueType & operator*() const { return *it_; }
bidirectional_iter & operator++()
{
++it_;
return *this;
}
bidirectional_iter & operator--()
{
--it_;
return *this;
}
friend bool
operator==(bidirectional_iter lhs, bidirectional_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::iterator_interface<
bidirectional_iter<ValueType>,
std::bidirectional_iterator_tag,
ValueType>;
using base_type::operator++;
using base_type::operator--;
private:
ValueType * it_;
template<typename ValueType2>
friend struct bidirectional_iter;
};
using bidirectional = bidirectional_iter<int>;
using const_bidirectional = bidirectional_iter<int const>;
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
bidirectional, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
bidirectional,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
const_bidirectional, std::bidirectional_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
const_bidirectional,
std::bidirectional_iterator_tag,
std::bidirectional_iterator_tag,
int,
int const &,
int const *,
std::ptrdiff_t)
std::array<int, 10> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
////////////////////
// view_interface //
////////////////////
#include "view_tests.hpp"
template<typename T>
using data_t = decltype(std::declval<T>().data());
static_assert(
ill_formed<
data_t,
subrange<
basic_bidirectional_iter,
basic_bidirectional_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
data_t,
subrange<
basic_bidirectional_iter,
basic_bidirectional_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using size_t_ = decltype(std::declval<T>().size());
static_assert(
ill_formed<
size_t_,
subrange<
basic_bidirectional_iter,
basic_bidirectional_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
size_t_,
subrange<
basic_bidirectional_iter,
basic_bidirectional_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using index_operator_t = decltype(std::declval<T>()[0]);
static_assert(
ill_formed<
index_operator_t,
subrange<
basic_bidirectional_iter,
basic_bidirectional_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
index_operator_t,
subrange<
basic_bidirectional_iter,
basic_bidirectional_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
int main()
{
{
basic_bidirectional_iter first(ints.data());
basic_bidirectional_iter last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> ints_copy;
std::copy(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
ints_copy.begin());
std::reverse(ints_copy.begin(), ints_copy.end());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> iota_ints;
basic_bidirectional_iter first(iota_ints.data());
basic_bidirectional_iter last(iota_ints.data() + iota_ints.size());
std::iota(first, last, 0);
BOOST_TEST(iota_ints == ints);
}
{
std::array<int, 10> iota_ints;
basic_bidirectional_iter first(iota_ints.data());
basic_bidirectional_iter last(iota_ints.data() + iota_ints.size());
std::iota(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
0);
std::reverse(iota_ints.begin(), iota_ints.end());
BOOST_TEST(iota_ints == ints);
}
}
{
basic_adapted_bidirectional_ptr_iter first(ints.data());
basic_adapted_bidirectional_ptr_iter last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> ints_copy;
std::copy(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
ints_copy.begin());
std::reverse(ints_copy.begin(), ints_copy.end());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> iota_ints;
basic_adapted_bidirectional_ptr_iter first(iota_ints.data());
basic_adapted_bidirectional_ptr_iter last(
iota_ints.data() + iota_ints.size());
std::iota(first, last, 0);
BOOST_TEST(iota_ints == ints);
}
{
std::array<int, 10> iota_ints;
basic_adapted_bidirectional_ptr_iter first(iota_ints.data());
basic_adapted_bidirectional_ptr_iter last(
iota_ints.data() + iota_ints.size());
std::iota(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
0);
std::reverse(iota_ints.begin(), iota_ints.end());
BOOST_TEST(iota_ints == ints);
}
}
{
std::list<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
basic_adapted_bidirectional_list_iter first(ints.begin());
basic_adapted_bidirectional_list_iter last(ints.end());
{
std::list<int> ints_copy;
std::copy(first, last, std::back_inserter(ints_copy));
BOOST_TEST(ints_copy == ints);
}
{
std::list<int> ints_copy;
std::copy(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
std::back_inserter(ints_copy));
std::reverse(ints_copy.begin(), ints_copy.end());
BOOST_TEST(ints_copy == ints);
}
}
{
{
bidirectional first(ints.data());
bidirectional last(ints.data() + ints.size());
const_bidirectional first_copy(first);
const_bidirectional last_copy(last);
std::equal(first, last, first_copy, last_copy);
}
{
adapted_bidirectional_ptr_iter<int> first(ints.data());
adapted_bidirectional_ptr_iter<int> last(ints.data() + ints.size());
adapted_bidirectional_ptr_iter<int const> first_copy(
(int const *)ints.data());
adapted_bidirectional_ptr_iter<int const> last_copy(
(int const *)ints.data() + ints.size());
std::equal(first, last, first_copy, last_copy);
}
}
{
{
bidirectional first(ints.data());
bidirectional last(ints.data() + ints.size());
const_bidirectional first_const(first);
const_bidirectional last_const(last);
BOOST_TEST(first == first_const);
BOOST_TEST(first_const == first);
BOOST_TEST(first != last_const);
BOOST_TEST(last_const != first);
}
{
adapted_bidirectional_ptr_iter<int> first(ints.data());
adapted_bidirectional_ptr_iter<int> last(ints.data() + ints.size());
adapted_bidirectional_ptr_iter<int const> first_const(
(int const *)ints.data());
adapted_bidirectional_ptr_iter<int const> last_const(
(int const *)ints.data() + ints.size());
static_assert(
!boost::stl_interfaces::v1_dtl::ra_iter<
adapted_bidirectional_ptr_iter<int>>::value,
"");
BOOST_TEST(first == first_const);
BOOST_TEST(first_const == first);
BOOST_TEST(first != last_const);
BOOST_TEST(last_const != first);
}
}
{
{
bidirectional first(ints.data());
bidirectional last(ints.data() + ints.size());
while (first != last && !(first == last))
first++;
}
{
bidirectional first(ints.data());
bidirectional last(ints.data() + ints.size());
while (first != last && !(first == last))
last--;
}
{
basic_bidirectional_iter first(ints.data());
basic_bidirectional_iter last(ints.data() + ints.size());
while (first != last && !(first == last))
first++;
}
{
basic_bidirectional_iter first(ints.data());
basic_bidirectional_iter last(ints.data() + ints.size());
while (first != last && !(first == last))
last--;
}
{
basic_adapted_bidirectional_ptr_iter first(ints.data());
basic_adapted_bidirectional_ptr_iter last(ints.data() + ints.size());
while (first != last && !(first == last))
first++;
}
{
basic_adapted_bidirectional_ptr_iter first(ints.data());
basic_adapted_bidirectional_ptr_iter last(ints.data() + ints.size());
while (first != last && !(first == last))
last--;
}
{
std::list<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
basic_adapted_bidirectional_list_iter first(ints.begin());
basic_adapted_bidirectional_list_iter last(ints.end());
while (first != last && !(first == last))
first++;
}
{
std::list<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
basic_adapted_bidirectional_list_iter first(ints.begin());
basic_adapted_bidirectional_list_iter last(ints.end());
while (first != last && !(first == last))
last--;
}
}
{
bidirectional first(ints.data());
bidirectional last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> ints_copy;
std::copy(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
ints_copy.begin());
std::reverse(ints_copy.begin(), ints_copy.end());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> iota_ints;
bidirectional first(iota_ints.data());
bidirectional last(iota_ints.data() + iota_ints.size());
std::iota(first, last, 0);
BOOST_TEST(iota_ints == ints);
}
{
std::array<int, 10> iota_ints;
bidirectional first(iota_ints.data());
bidirectional last(iota_ints.data() + iota_ints.size());
std::iota(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
0);
std::reverse(iota_ints.begin(), iota_ints.end());
BOOST_TEST(iota_ints == ints);
}
}
{
const_bidirectional first(ints.data());
const_bidirectional last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
BOOST_TEST(std::binary_search(first, last, 3));
BOOST_TEST(std::binary_search(
std::make_reverse_iterator(last),
std::make_reverse_iterator(first),
3,
std::greater<>{}));
}
}
{
basic_bidirectional_iter first(ints.data());
basic_bidirectional_iter last(ints.data() + ints.size());
auto r = range<boost::stl_interfaces::v1::element_layout::discontiguous>(
first, last);
auto empty =
range<boost::stl_interfaces::v1::element_layout::discontiguous>(
first, first);
// range begin/end
{
std::array<int, 10> ints_copy;
std::copy(r.begin(), r.end(), ints_copy.begin());
BOOST_TEST(ints_copy == ints);
BOOST_TEST(empty.begin() == empty.end());
}
// empty/op bool
{
BOOST_TEST(!r.empty());
BOOST_TEST(r);
BOOST_TEST(empty.empty());
BOOST_TEST(!empty);
auto const cr = r;
BOOST_TEST(!cr.empty());
BOOST_TEST(cr);
auto const cempty = empty;
BOOST_TEST(cempty.empty());
BOOST_TEST(!cempty);
}
// front/back
{
BOOST_TEST(r.front() == 0);
BOOST_TEST(r.back() == 9);
auto const cr = r;
BOOST_TEST(cr.front() == 0);
BOOST_TEST(cr.back() == 9);
}
}
return boost::report_errors();
}

View File

@@ -0,0 +1,103 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <boost/stl_interfaces/view_interface.hpp>
#include <boost/stl_interfaces/sequence_container_interface.hpp>
#include <boost/core/lightweight_test.hpp>
#include <array>
#include <list>
#include <vector>
namespace detail = boost::stl_interfaces::detail;
namespace v1_dtl = boost::stl_interfaces::v1::v1_dtl;
// iter_difference_t
static_assert(
std::is_same<v1_dtl::iter_difference_t<int *>, std::ptrdiff_t>::value, "");
static_assert(std::is_same<
v1_dtl::iter_difference_t<std::vector<double>::iterator>,
std::vector<double>::difference_type>::value, "");
static_assert(std::is_same<
v1_dtl::iter_difference_t<std::list<double>::iterator>,
std::list<double>::difference_type>::value, "");
struct ridiculous_range
{
int * begin() { return nullptr; }
double end() { return 1.3; }
};
// iterator_t
static_assert(std::is_same<
v1_dtl::iterator_t<std::vector<double>>,
std::vector<double>::iterator>::value, "");
static_assert(std::is_same<
v1_dtl::iterator_t<std::list<double>>,
std::list<double>::iterator>::value, "");
static_assert(std::is_same<v1_dtl::iterator_t<ridiculous_range>, int *>::value, "");
// sentinel_t
static_assert(std::is_same<
v1_dtl::sentinel_t<std::vector<double>>,
std::vector<double>::iterator>::value, "");
static_assert(std::is_same<
v1_dtl::sentinel_t<std::list<double>>,
std::list<double>::iterator>::value, "");
static_assert(std::is_same<v1_dtl::sentinel_t<ridiculous_range>, double>::value, "");
// range_difference_t
static_assert(
std::is_same<
v1_dtl::range_difference_t<std::vector<double>>,
std::iterator_traits<std::vector<double>::iterator>::difference_type>::value, "");
static_assert(
std::is_same<
v1_dtl::range_difference_t<std::list<double>>,
std::iterator_traits<std::list<double>::iterator>::difference_type>::value, "");
static_assert(std::is_same<
v1_dtl::range_difference_t<ridiculous_range>,
std::ptrdiff_t>::value, "");
// common_range
static_assert(v1_dtl::common_range<std::vector<double>>::value, "");
static_assert(v1_dtl::common_range<std::list<double>>::value, "");
static_assert(!v1_dtl::common_range<ridiculous_range>::value, "");
struct no_clear
{};
int main()
{
{
{
no_clear nc;
v1_dtl::clear_impl<no_clear>::call(nc);
}
{
std::vector<int> vec(10);
v1_dtl::clear_impl<std::vector<int>>::call(vec);
BOOST_TEST(vec.empty());
}
}
{
std::array<int, 5> ints = {{0, 1, 2, 3, 4}};
int const new_value = 6;
detail::n_iter<int, int> first = detail::make_n_iter(new_value, 3);
detail::n_iter<int, int> last = detail::make_n_iter_end(new_value, 3);
std::copy(first, last, &ints[1]);
BOOST_TEST(ints == (std::array<int, 5>{{0, 6, 6, 6, 4}}));
}
return boost::report_errors();
}

View File

@@ -0,0 +1,342 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include "ill_formed.hpp"
#include <boost/core/lightweight_test.hpp>
#include <algorithm>
#include <array>
#include <numeric>
#include <type_traits>
template<typename T>
using decrementable_t = decltype(--std::declval<T &>());
struct basic_forward_iter
: boost::stl_interfaces::
iterator_interface<basic_forward_iter, std::forward_iterator_tag, int>
{
basic_forward_iter() : it_(nullptr) {}
basic_forward_iter(int * it) : it_(it) {}
int & operator*() const { return *it_; }
basic_forward_iter & operator++()
{
++it_;
return *this;
}
friend bool
operator==(basic_forward_iter lhs, basic_forward_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::
iterator_interface<basic_forward_iter, std::forward_iterator_tag, int>;
using base_type::operator++;
private:
int * it_;
};
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
basic_forward_iter, std::forward_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
basic_forward_iter,
std::forward_iterator_tag,
std::forward_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
static_assert(ill_formed<decrementable_t, basic_forward_iter>::value, "");
template<typename ValueType>
struct forward_iter : boost::stl_interfaces::iterator_interface<
forward_iter<ValueType>,
std::forward_iterator_tag,
ValueType>
{
forward_iter() : it_(nullptr) {}
forward_iter(ValueType * it) : it_(it) {}
template<
typename ValueType2,
typename E = std::enable_if_t<
std::is_convertible<ValueType2 *, ValueType *>::value>>
forward_iter(forward_iter<ValueType2> it) : it_(it.it_)
{}
ValueType & operator*() const { return *it_; }
forward_iter & operator++()
{
++it_;
return *this;
}
friend bool operator==(forward_iter lhs, forward_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::iterator_interface<
forward_iter<ValueType>,
std::forward_iterator_tag,
ValueType>;
using base_type::operator++;
private:
ValueType * it_;
template<typename ValueType2>
friend struct forward_iter;
};
using forward = forward_iter<int>;
using const_forward = forward_iter<int const>;
static_assert(ill_formed<decrementable_t, forward>::value, "");
static_assert(ill_formed<decrementable_t, const_forward>::value, "");
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(forward, std::forward_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
forward,
std::forward_iterator_tag,
std::forward_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(const_forward, std::forward_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
const_forward,
std::forward_iterator_tag,
std::forward_iterator_tag,
int,
int const &,
int const *,
std::ptrdiff_t)
std::array<int, 10> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
////////////////////
// view_interface //
////////////////////
#include "view_tests.hpp"
template<typename T>
using data_t = decltype(std::declval<T>().data());
static_assert(
ill_formed<
data_t,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
data_t,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using size_t_ = decltype(std::declval<T>().size());
static_assert(
ill_formed<
size_t_,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
size_t_,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using back_t_ = decltype(std::declval<T>().back());
static_assert(
ill_formed<
back_t_,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
back_t_,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using index_operator_t = decltype(std::declval<T>()[0]);
static_assert(
ill_formed<
index_operator_t,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
index_operator_t,
subrange<
basic_forward_iter,
basic_forward_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
int main()
{
{
basic_forward_iter first(ints.data());
basic_forward_iter last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> iota_ints;
basic_forward_iter first(iota_ints.data());
basic_forward_iter last(iota_ints.data() + iota_ints.size());
std::iota(first, last, 0);
BOOST_TEST(iota_ints == ints);
}
}
{
forward first(ints.data());
forward last(ints.data() + ints.size());
const_forward first_copy(first);
const_forward last_copy(last);
std::equal(first, last, first_copy, last_copy);
}
{
forward first(ints.data());
forward last(ints.data() + ints.size());
while (first != last)
first++;
}
{
forward first(ints.data());
forward last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<int, 10> iota_ints;
forward first(iota_ints.data());
forward last(iota_ints.data() + iota_ints.size());
std::iota(first, last, 0);
BOOST_TEST(iota_ints == ints);
}
}
{
const_forward first(ints.data());
const_forward last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
BOOST_TEST(std::binary_search(first, last, 3));
}
}
{
basic_forward_iter first(ints.data());
basic_forward_iter last(ints.data() + ints.size());
auto r = range<boost::stl_interfaces::v1::element_layout::discontiguous>(
first, last);
auto empty =
range<boost::stl_interfaces::v1::element_layout::discontiguous>(
first, first);
// range begin/end
{
std::array<int, 10> ints_copy;
std::copy(r.begin(), r.end(), ints_copy.begin());
BOOST_TEST(ints_copy == ints);
BOOST_TEST(empty.begin() == empty.end());
}
// empty/op bool
{
BOOST_TEST(!r.empty());
BOOST_TEST(r);
BOOST_TEST(empty.empty());
BOOST_TEST(!empty);
auto const cr = r;
BOOST_TEST(!cr.empty());
BOOST_TEST(cr);
auto const cempty = empty;
BOOST_TEST(cempty.empty());
BOOST_TEST(!cempty);
}
// front/back
{
BOOST_TEST(r.front() == 0);
auto const cr = r;
BOOST_TEST(cr.front() == 0);
}
}
return boost::report_errors();
}

View File

@@ -0,0 +1,17 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#ifndef BOOST_STL_INTERFACES_ILL_FORMED_HPP
#define BOOST_STL_INTERFACES_ILL_FORMED_HPP
#include <boost/stl_interfaces/iterator_interface.hpp>
template<template<class...> class Template, typename... Args>
using ill_formed = std::integral_constant<
bool,
!boost::stl_interfaces::detail::detector<void, Template, Args...>::value>;
#endif

View File

@@ -0,0 +1,420 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include "ill_formed.hpp"
#include <boost/core/lightweight_test.hpp>
#include <array>
#include <numeric>
#include <type_traits>
struct basic_input_iter
: boost::stl_interfaces::
iterator_interface<basic_input_iter, std::input_iterator_tag, int>
{
basic_input_iter() : it_(nullptr) {}
basic_input_iter(int * it) : it_(it) {}
int & operator*() const noexcept { return *it_; }
basic_input_iter & operator++() noexcept
{
++it_;
return *this;
}
friend bool operator==(basic_input_iter lhs, basic_input_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::
iterator_interface<basic_input_iter, std::input_iterator_tag, int>;
using base_type::operator++;
private:
int * it_;
};
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
basic_input_iter, std::input_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
basic_input_iter,
std::input_iterator_tag,
std::input_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
template<typename ValueType>
struct input_iter : boost::stl_interfaces::iterator_interface<
input_iter<ValueType>,
std::input_iterator_tag,
ValueType>
{
input_iter() : it_(nullptr) {}
input_iter(ValueType * it) : it_(it) {}
template<
typename ValueType2,
typename E = std::enable_if_t<
std::is_convertible<ValueType2 *, ValueType *>::value>>
input_iter(input_iter<ValueType2> it) : it_(it.it_)
{}
ValueType & operator*() const noexcept { return *it_; }
input_iter & operator++() noexcept
{
++it_;
return *this;
}
friend bool operator==(input_iter lhs, input_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::iterator_interface<
input_iter<ValueType>,
std::input_iterator_tag,
ValueType>;
using base_type::operator++;
private:
ValueType * it_;
template<typename ValueType2>
friend struct input_iter;
};
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(input_iter<int>, std::input_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
input_iter<int>,
std::input_iterator_tag,
std::input_iterator_tag,
int,
int &,
int *,
std::ptrdiff_t)
using int_input = input_iter<int>;
using const_int_input = input_iter<int const>;
using pair_input = input_iter<std::pair<int, int>>;
using const_pair_input = input_iter<std::pair<int, int> const>;
template<typename ValueType>
struct proxy_input_iter : boost::stl_interfaces::proxy_iterator_interface<
proxy_input_iter<ValueType>,
std::input_iterator_tag,
ValueType>
{
proxy_input_iter() : it_(nullptr) {}
proxy_input_iter(ValueType * it) : it_(it) {}
template<
typename ValueType2,
typename E = std::enable_if_t<
std::is_convertible<ValueType2 *, ValueType *>::value>>
proxy_input_iter(proxy_input_iter<ValueType2> it) : it_(it.it_)
{}
ValueType operator*() const noexcept { return *it_; }
proxy_input_iter & operator++() noexcept
{
++it_;
return *this;
}
friend bool operator==(proxy_input_iter lhs, proxy_input_iter rhs) noexcept
{
return lhs.it_ == rhs.it_;
}
using base_type = boost::stl_interfaces::proxy_iterator_interface<
proxy_input_iter<ValueType>,
std::input_iterator_tag,
ValueType>;
using base_type::operator++;
private:
ValueType * it_;
template<typename ValueType2>
friend struct proxy_input_iter;
};
using int_pair = std::pair<int, int>;
BOOST_STL_INTERFACES_STATIC_ASSERT_CONCEPT(
proxy_input_iter<int_pair>, std::input_iterator)
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
proxy_input_iter<int_pair>,
std::input_iterator_tag,
std::input_iterator_tag,
int_pair,
int_pair,
boost::stl_interfaces::proxy_arrow_result<int_pair>,
std::ptrdiff_t)
std::array<int, 10> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
std::array<std::pair<int, int>, 10> pairs = {{
{0, 1},
{1, 1},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{6, 1},
{7, 1},
{8, 1},
{9, 1},
}};
////////////////////
// view_interface //
////////////////////
#include "view_tests.hpp"
template<typename T>
using data_t = decltype(std::declval<T>().data());
static_assert(
ill_formed<
data_t,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
data_t,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using size_t_ = decltype(std::declval<T>().size());
static_assert(
ill_formed<
size_t_,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
size_t_,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using back_t_ = decltype(std::declval<T>().back());
static_assert(
ill_formed<
back_t_,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
back_t_,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
template<typename T>
using index_operator_t = decltype(std::declval<T>()[0]);
static_assert(
ill_formed<
index_operator_t,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous>>::value,
"");
static_assert(
ill_formed<
index_operator_t,
subrange<
basic_input_iter,
basic_input_iter,
boost::stl_interfaces::v1::element_layout::discontiguous> const>::
value,
"");
int main()
{
{
basic_input_iter first(ints.data());
basic_input_iter last(ints.data() + ints.size());
{
std::array<int, 10> ints_copy;
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
}
{
int_input first(ints.data());
int_input last(ints.data() + ints.size());
const_int_input first_copy(first);
const_int_input last_copy(last);
std::equal(first, last, first_copy, last_copy);
}
{
int_input first(ints.data());
int_input last(ints.data() + ints.size());
while (first != last)
first++;
}
{
{
std::array<int, 10> ints_copy;
int_input first(ints.data());
int_input last(ints.data() + ints.size());
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<std::pair<int, int>, 10> pairs_copy;
pair_input first(pairs.data());
pair_input last(pairs.data() + pairs.size());
std::copy(first, last, pairs_copy.begin());
BOOST_TEST(pairs_copy == pairs);
}
{
std::array<int, 10> firsts_copy;
pair_input first(pairs.data());
pair_input last(pairs.data() + pairs.size());
for (auto out = firsts_copy.begin(); first != last; ++first) {
*out++ = first->first;
}
BOOST_TEST(firsts_copy == ints);
}
{
std::array<int, 10> firsts_copy;
proxy_input_iter<std::pair<int, int>> first(pairs.data());
proxy_input_iter<std::pair<int, int>> last(pairs.data() + pairs.size());
for (auto out = firsts_copy.begin(); first != last; ++first) {
*out++ = first->first;
}
BOOST_TEST(firsts_copy == ints);
}
}
{
{
std::array<int, 10> ints_copy;
const_int_input first(ints.data());
const_int_input last(ints.data() + ints.size());
std::copy(first, last, ints_copy.begin());
BOOST_TEST(ints_copy == ints);
}
{
std::array<std::pair<int, int>, 10> pairs_copy;
const_pair_input first(pairs.data());
const_pair_input last(pairs.data() + pairs.size());
std::copy(first, last, pairs_copy.begin());
BOOST_TEST(pairs_copy == pairs);
}
{
std::array<int, 10> firsts_copy;
const_pair_input first(pairs.data());
const_pair_input last(pairs.data() + pairs.size());
for (auto out = firsts_copy.begin(); first != last; ++first) {
*out++ = first->first;
}
BOOST_TEST(firsts_copy == ints);
}
{
std::array<int, 10> firsts_copy;
proxy_input_iter<std::pair<int, int> const> first(pairs.data());
proxy_input_iter<std::pair<int, int> const> last(
pairs.data() + pairs.size());
for (auto out = firsts_copy.begin(); first != last; ++first) {
*out++ = first->first;
}
BOOST_TEST(firsts_copy == ints);
}
}
{
basic_input_iter first(ints.data());
basic_input_iter last(ints.data() + ints.size());
auto r = range<boost::stl_interfaces::v1::element_layout::discontiguous>(
first, last);
auto empty =
range<boost::stl_interfaces::v1::element_layout::discontiguous>(
first, first);
// range begin/end
{
std::array<int, 10> ints_copy;
std::copy(r.begin(), r.end(), ints_copy.begin());
BOOST_TEST(ints_copy == ints);
BOOST_TEST(empty.begin() == empty.end());
}
// empty/op bool
{
BOOST_TEST(!r.empty());
BOOST_TEST(r);
BOOST_TEST(empty.empty());
BOOST_TEST(!empty);
auto const cr = r;
BOOST_TEST(!cr.empty());
BOOST_TEST(cr);
auto const cempty = empty;
BOOST_TEST(cempty.empty());
BOOST_TEST(!cempty);
}
// front/back
{
BOOST_TEST(r.front() == 0);
auto const cr = r;
BOOST_TEST(cr.front() == 0);
}
}
return boost::report_errors();
}

View File

@@ -0,0 +1,130 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <boost/core/lightweight_test.hpp>
#include <array>
#include <numeric>
#include <vector>
#include <type_traits>
struct basic_output_iter
: boost::stl_interfaces::
iterator_interface<basic_output_iter, std::output_iterator_tag, int>
{
basic_output_iter() : it_(nullptr) {}
basic_output_iter(int * it) : it_(it) {}
int & operator*() noexcept { return *it_; }
basic_output_iter & operator++() noexcept
{
++it_;
return *this;
}
using base_type = boost::stl_interfaces::
iterator_interface<basic_output_iter, std::output_iterator_tag, int>;
using base_type::operator++;
private:
int * it_;
};
using output = basic_output_iter;
#if 201703L < __cplusplus && defined(__cpp_lib_concepts)
static_assert(std::output_iterator<output, int>, "");
#endif
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
output,
std::output_iterator_tag,
std::output_iterator_tag,
int,
int &,
void,
std::ptrdiff_t)
template<typename Container>
struct back_insert_iter : boost::stl_interfaces::iterator_interface<
back_insert_iter<Container>,
std::output_iterator_tag,
typename Container::value_type,
back_insert_iter<Container> &>
{
back_insert_iter() : c_(nullptr) {}
back_insert_iter(Container & c) : c_(std::addressof(c)) {}
back_insert_iter & operator*() noexcept { return *this; }
back_insert_iter & operator++() noexcept { return *this; }
back_insert_iter & operator=(typename Container::value_type const & v)
{
c_->push_back(v);
return *this;
}
back_insert_iter & operator=(typename Container::value_type && v)
{
c_->push_back(std::move(v));
return *this;
}
using base_type = boost::stl_interfaces::iterator_interface<
back_insert_iter<Container>,
std::output_iterator_tag,
typename Container::value_type,
back_insert_iter<Container> &>;
using base_type::operator++;
private:
Container * c_;
};
using back_insert = back_insert_iter<std::vector<int>>;
#if 201703L < __cplusplus && defined(__cpp_lib_concepts)
static_assert(std::output_iterator<back_insert, int>, "");
#endif
BOOST_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS(
back_insert,
std::output_iterator_tag,
std::output_iterator_tag,
int,
back_insert &,
void,
std::ptrdiff_t)
std::vector<int> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
int main()
{
{
std::vector<int> ints_copy(ints.size());
std::copy(ints.begin(), ints.end(), output(&ints_copy[0]));
BOOST_TEST(ints_copy == ints);
}
{
std::vector<int> ints_copy;
std::copy(ints.begin(), ints.end(), back_insert(ints_copy));
BOOST_TEST(ints_copy == ints);
}
{
std::vector<int> ints_copy;
back_insert out(ints_copy);
for (int i = 0; i < 10; ++i)
out++;
}
return boost::report_errors();
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,243 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include <boost/stl_interfaces/iterator_interface.hpp>
#include <boost/stl_interfaces/reverse_iterator.hpp>
#include <boost/core/lightweight_test.hpp>
#include <algorithm>
#include <array>
#include <list>
#include <tuple>
#include <vector>
struct zip_iter : boost::stl_interfaces::proxy_iterator_interface<
zip_iter,
std::random_access_iterator_tag,
std::tuple<int, int>,
std::tuple<int &, int &>>
{
zip_iter() : it1_(nullptr), it2_(nullptr) {}
zip_iter(int * it1, int * it2) : it1_(it1), it2_(it2) {}
std::tuple<int &, int &> operator*() const
{
return std::tuple<int &, int &>{*it1_, *it2_};
}
zip_iter & operator+=(std::ptrdiff_t i)
{
it1_ += i;
it2_ += i;
return *this;
}
friend std::ptrdiff_t operator-(zip_iter lhs, zip_iter rhs) noexcept
{
return lhs.it1_ - rhs.it1_;
}
private:
int * it1_;
int * it2_;
};
int main()
{
{
std::list<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto first = boost::stl_interfaces::make_reverse_iterator(ints.end());
auto last = boost::stl_interfaces::make_reverse_iterator(ints.begin());
{
auto cfirst = boost::stl_interfaces::reverse_iterator<
std::list<int>::const_iterator>(first);
auto clast = boost::stl_interfaces::reverse_iterator<
std::list<int>::const_iterator>(last);
BOOST_TEST(std::equal(first, last, cfirst, clast));
}
{
auto ints_copy = ints;
std::reverse(ints_copy.begin(), ints_copy.end());
BOOST_TEST(
std::equal(first, last, ints_copy.begin(), ints_copy.end()));
}
{
std::list<int> ints_copy;
std::reverse_copy(first, last, std::back_inserter(ints_copy));
BOOST_TEST(ints_copy == ints);
}
{
std::size_t count = 0;
for (auto it = first; it != last; ++it) {
++count;
}
BOOST_TEST(count == ints.size());
}
{
std::size_t count = 0;
for (auto it = first; it != last; it++) {
++count;
}
BOOST_TEST(count == ints.size());
}
{
std::size_t count = 0;
for (auto it = last; it != first; --it) {
++count;
}
BOOST_TEST(count == ints.size());
}
{
std::size_t count = 0;
for (auto it = last; it != first; it--) {
++count;
}
BOOST_TEST(count == ints.size());
}
}
{
std::vector<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto first = boost::stl_interfaces::make_reverse_iterator(ints.end());
auto last = boost::stl_interfaces::make_reverse_iterator(ints.begin());
{
auto cfirst = boost::stl_interfaces::reverse_iterator<
std::vector<int>::const_iterator>(first);
auto clast = boost::stl_interfaces::reverse_iterator<
std::vector<int>::const_iterator>(last);
BOOST_TEST(std::equal(first, last, cfirst, clast));
}
{
auto ints_copy = ints;
std::reverse(ints_copy.begin(), ints_copy.end());
BOOST_TEST(first - last == ints_copy.begin() - ints_copy.end());
BOOST_TEST(
std::equal(first, last, ints_copy.begin(), ints_copy.end()));
}
{
std::vector<int> ints_copy;
std::reverse_copy(first, last, std::back_inserter(ints_copy));
BOOST_TEST(ints_copy == ints);
}
{
std::size_t count = 0;
for (auto it = first; it != last; ++it) {
++count;
}
BOOST_TEST(count == ints.size());
}
{
std::size_t count = 0;
for (auto it = first; it != last; it++) {
++count;
}
BOOST_TEST(count == ints.size());
}
{
std::size_t count = 0;
for (auto it = last; it != first; --it) {
++count;
}
BOOST_TEST(count == ints.size());
}
{
std::size_t count = 0;
for (auto it = last; it != first; it--) {
++count;
}
BOOST_TEST(count == ints.size());
}
}
{
std::array<int, 10> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
std::array<int, 10> ones = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
std::array<std::tuple<int, int>, 10> tuples = {{
{0, 1},
{1, 1},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{6, 1},
{7, 1},
{8, 1},
{9, 1},
}};
auto first = boost::stl_interfaces::make_reverse_iterator(
zip_iter(ints.data() + ints.size(), ones.data() + ones.size()));
auto last = boost::stl_interfaces::make_reverse_iterator(
zip_iter(ints.data(), ones.data()));
{
auto tuples_copy = tuples;
std::reverse(tuples_copy.begin(), tuples_copy.end());
BOOST_TEST(first - last == tuples_copy.begin() - tuples_copy.end());
BOOST_TEST(
std::equal(first, last, tuples_copy.begin(), tuples_copy.end()));
}
{
std::array<std::tuple<int, int>, 10> tuples_copy;
std::reverse_copy(first, last, tuples_copy.begin());
BOOST_TEST(tuples_copy == tuples);
}
{
std::size_t count = 0;
for (auto it = first; it != last; ++it) {
++count;
}
BOOST_TEST(count == tuples.size());
}
{
std::size_t count = 0;
for (auto it = first; it != last; it++) {
++count;
}
BOOST_TEST(count == tuples.size());
}
{
std::size_t count = 0;
for (auto it = last; it != first; --it) {
++count;
}
BOOST_TEST(count == tuples.size());
}
{
std::size_t count = 0;
for (auto it = last; it != first; it--) {
++count;
}
BOOST_TEST(count == tuples.size());
}
}
return boost::report_errors();
}

View File

@@ -0,0 +1,773 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include "../example/static_vector.hpp"
#include "ill_formed.hpp"
#include <boost/core/lightweight_test.hpp>
#include <array>
// Instantiate all the members we can.
template struct static_vector<int, 1024>;
using vec_type = static_vector<int, 10>;
void test_default_ctor()
{
vec_type v;
BOOST_TEST(v.empty());
BOOST_TEST(v.size() == 0u);
BOOST_TEST(v.max_size() == 10u);
BOOST_TEST(v.capacity() == 10u);
BOOST_TEST(v == v);
BOOST_TEST(v <= v);
BOOST_TEST(v >= v);
BOOST_TEST_THROWS(v.at(0), std::out_of_range);
vec_type const & cv = v;
BOOST_TEST(cv.empty());
BOOST_TEST(cv.size() == 0u);
BOOST_TEST(cv.max_size() == 10u);
BOOST_TEST(cv.capacity() == 10u);
BOOST_TEST(cv == cv);
BOOST_TEST(cv <= cv);
BOOST_TEST(cv >= cv);
BOOST_TEST_THROWS(cv.at(0), std::out_of_range);
}
void test_other_ctors_assign_ctor()
{
{
vec_type v(3);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
vec_type v2(std::initializer_list<int>{0, 0, 0});
BOOST_TEST(v == v2);
}
{
std::initializer_list<int> il{3, 2, 1};
vec_type v(il);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(std::is_same<decltype(v = il), vec_type &>::value, "");
vec_type v2;
v2 = il;
BOOST_TEST(v == v2);
}
{
std::initializer_list<int> il{3, 2, 1};
vec_type v;
v.assign(il);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(std::is_same<decltype(v.assign(il)), void>::value, "");
vec_type v2;
v2 = il;
BOOST_TEST(v == v2);
}
{
vec_type v(3, 4);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
vec_type v2 = {4, 4, 4};
BOOST_TEST(v == v2);
}
{
vec_type v;
v.assign(3, 4);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(std::is_same<decltype(v.assign(3, 4)), void>::value, "");
vec_type v2 = {4, 4, 4};
BOOST_TEST(v == v2);
}
{
std::array<int, 3> a = {{1, 2, 3}};
vec_type v(a.begin(), a.end());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
vec_type v2 = {1, 2, 3};
BOOST_TEST(v == v2);
}
{
std::array<int, 3> a = {{1, 2, 3}};
vec_type v;
v.assign(a.begin(), a.end());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(
std::is_same<decltype(v.assign(a.begin(), a.end())), void>::value,
"");
vec_type v2 = {1, 2, 3};
BOOST_TEST(v == v2);
}
}
void test_resize()
{
{
vec_type v;
static_assert(std::is_same<decltype(v.resize(1)), void>::value, "");
v.resize(3);
BOOST_TEST(v == vec_type(3));
v.resize(6);
BOOST_TEST(v == vec_type(6));
}
{
vec_type v(6);
v.resize(3);
BOOST_TEST(v == vec_type(3));
v.resize(0);
BOOST_TEST(v == vec_type{});
}
}
void test_assignment_copy_move_equality()
{
{
vec_type v2 = {4, 4, 4};
vec_type v(v2);
BOOST_TEST(v == v2);
}
{
vec_type v;
vec_type v2 = {4, 4, 4};
static_assert(std::is_same<decltype(v = v2), vec_type &>::value, "");
static_assert(
std::is_same<decltype(v = std::move(v2)), vec_type &>::value, "");
v = v2;
BOOST_TEST(v == v2);
}
{
vec_type v2 = {4, 4, 4};
vec_type v(std::move(v2));
BOOST_TEST(v == (vec_type(3, 4)));
BOOST_TEST(v2.empty());
}
{
vec_type v;
vec_type v2 = {4, 4, 4};
v = std::move(v2);
BOOST_TEST(v == (vec_type(3, 4)));
BOOST_TEST(v2.empty());
}
}
void test_comparisons()
{
vec_type sm = {1, 2, 3};
vec_type md = {1, 2, 3, 4};
vec_type lg = {1, 2, 3, 4, 5};
BOOST_TEST(sm == sm);
BOOST_TEST(!(sm == md));
BOOST_TEST(!(sm == lg));
BOOST_TEST(!(sm != sm));
BOOST_TEST(sm != md);
BOOST_TEST(sm != lg);
BOOST_TEST(!(sm < sm));
BOOST_TEST(sm < md);
BOOST_TEST(sm < lg);
BOOST_TEST(sm <= sm);
BOOST_TEST(sm <= md);
BOOST_TEST(sm <= lg);
BOOST_TEST(!(sm > sm));
BOOST_TEST(!(sm > md));
BOOST_TEST(!(sm > lg));
BOOST_TEST(sm >= sm);
BOOST_TEST(!(sm >= md));
BOOST_TEST(!(sm >= lg));
BOOST_TEST(!(md == sm));
BOOST_TEST(md == md);
BOOST_TEST(!(md == lg));
BOOST_TEST(!(md < sm));
BOOST_TEST(!(md < md));
BOOST_TEST(md < lg);
BOOST_TEST(!(md <= sm));
BOOST_TEST(md <= md);
BOOST_TEST(md <= lg);
BOOST_TEST(md > sm);
BOOST_TEST(!(md > md));
BOOST_TEST(!(md > lg));
BOOST_TEST(md >= sm);
BOOST_TEST(md >= md);
BOOST_TEST(!(md >= lg));
BOOST_TEST(!(lg == sm));
BOOST_TEST(!(lg == md));
BOOST_TEST(lg == lg);
BOOST_TEST(!(lg < sm));
BOOST_TEST(!(lg < md));
BOOST_TEST(!(lg < lg));
BOOST_TEST(!(lg <= sm));
BOOST_TEST(!(lg <= md));
BOOST_TEST(lg <= lg);
BOOST_TEST(lg > sm);
BOOST_TEST(lg > md);
BOOST_TEST(!(lg > lg));
BOOST_TEST(lg >= sm);
BOOST_TEST(lg >= md);
BOOST_TEST(lg >= lg);
}
void test_swap()
{
{
vec_type v1(3, 4);
vec_type v2(4, 3);
static_assert(std::is_same<decltype(v1.swap(v2)), void>::value, "");
static_assert(std::is_same<decltype(swap(v1, v2)), void>::value, "");
v1.swap(v2);
BOOST_TEST(v1.size() == 4u);
BOOST_TEST(v2.size() == 3u);
BOOST_TEST(v1 == vec_type(4, 3));
BOOST_TEST(v2 == vec_type(3, 4));
}
{
vec_type v1(3, 4);
vec_type v2(4, 3);
swap(v1, v2);
BOOST_TEST(v1.size() == 4u);
BOOST_TEST(v2.size() == 3u);
BOOST_TEST(v1 == vec_type(4, 3));
BOOST_TEST(v2 == vec_type(3, 4));
}
}
template<typename Iter>
using writable_iter_t = decltype(
*std::declval<Iter>() =
std::declval<typename std::iterator_traits<Iter>::value_type>());
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().begin())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().end())>::value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<vec_type &>().cbegin())>::
value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<vec_type &>().cend())>::
value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().rbegin())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().rend())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type &>().crbegin())>::value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<vec_type &>().crend())>::
value,
"");
void test_iterators()
{
{
vec_type v = {3, 2, 1};
static_assert(
std::is_same<decltype(v.begin()), vec_type::iterator>::value, "");
static_assert(
std::is_same<decltype(v.end()), vec_type::iterator>::value, "");
static_assert(
std::is_same<decltype(v.cbegin()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cend()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.rbegin()), vec_type::reverse_iterator>::
value,
"");
static_assert(
std::is_same<decltype(v.rbegin()), vec_type::reverse_iterator>::
value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
std::array<int, 3> const a = {{3, 2, 1}};
std::array<int, 3> const ra = {{1, 2, 3}};
BOOST_TEST(std::equal(v.begin(), v.end(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.cbegin(), v.cend(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.rbegin(), v.rend(), ra.begin(), ra.end()));
BOOST_TEST(std::equal(v.crbegin(), v.crend(), ra.begin(), ra.end()));
*v.begin() = 8;
*v.rbegin() = 9;
BOOST_TEST(v == vec_type({8, 2, 9}));
}
{
vec_type const v = {3, 2, 1};
static_assert(
std::is_same<decltype(v.begin()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.end()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cbegin()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cend()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.rbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.rbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
std::array<int, 3> const a = {{3, 2, 1}};
std::array<int, 3> const ra = {{1, 2, 3}};
BOOST_TEST(std::equal(v.begin(), v.end(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.cbegin(), v.cend(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.rbegin(), v.rend(), ra.begin(), ra.end()));
BOOST_TEST(std::equal(v.crbegin(), v.crend(), ra.begin(), ra.end()));
}
}
void test_emplace_insert()
{
{
vec_type v;
int const i = 0;
static_assert(
std::is_same<decltype(v.emplace_back(0)), vec_type::reference>::
value,
"");
static_assert(
std::is_same<decltype(v.emplace_back(i)), vec_type::reference>::
value,
"");
v.emplace_back(i);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == i);
v.emplace_back(1);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 1);
v.emplace_back(2);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 2);
BOOST_TEST(v == vec_type({0, 1, 2}));
}
{
vec_type v = {1, 2};
int const i = 0;
static_assert(
std::is_same<
decltype(v.emplace(v.begin(), 0)),
vec_type::iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.emplace(v.begin(), i)),
vec_type::iterator>::value,
"");
v.emplace(v.begin(), i);
BOOST_TEST(v == vec_type({0, 1, 2}));
v.emplace(v.end(), 3);
BOOST_TEST(v == vec_type({0, 1, 2, 3}));
v.emplace(v.begin() + 2, 9);
BOOST_TEST(v == vec_type({0, 1, 9, 2, 3}));
}
{
vec_type v = {1, 2};
std::array<int, 2> a1 = {{0, 0}};
std::array<int, 1> a2 = {{3}};
std::array<int, 3> a3 = {{9, 9, 9}};
static_assert(
std::is_same<
decltype(v.insert(v.begin(), a1.begin(), a1.end())),
vec_type::iterator>::value,
"");
auto const it0 = v.insert(v.begin(), a1.begin(), a1.end());
BOOST_TEST(v == vec_type({0, 0, 1, 2}));
BOOST_TEST(it0 == v.begin());
auto const it1 = v.insert(v.end(), a2.begin(), a2.end());
BOOST_TEST(v == vec_type({0, 0, 1, 2, 3}));
BOOST_TEST(it1 == v.begin() + 4);
auto const it2 = v.insert(v.begin() + 2, a3.begin(), a3.end());
BOOST_TEST(v == vec_type({0, 0, 9, 9, 9, 1, 2, 3}));
BOOST_TEST(it2 == v.begin() + 2);
}
{
vec_type v = {1, 2};
int const i = 0;
static_assert(
std::is_same<decltype(v.insert(v.begin(), 0)), vec_type::iterator>::
value,
"");
static_assert(
std::is_same<decltype(v.insert(v.begin(), i)), vec_type::iterator>::
value,
"");
v.insert(v.begin(), i);
BOOST_TEST(v == vec_type({0, 1, 2}));
v.insert(v.end(), 3);
BOOST_TEST(v == vec_type({0, 1, 2, 3}));
v.insert(v.begin() + 2, 9);
BOOST_TEST(v == vec_type({0, 1, 9, 2, 3}));
}
{
vec_type v = {1, 2};
static_assert(
std::is_same<
decltype(v.insert(v.begin(), 3, 0)),
vec_type::iterator>::value,
"");
v.insert(v.begin(), 2, 0);
BOOST_TEST(v == vec_type({0, 0, 1, 2}));
v.insert(v.end(), 1, 3);
BOOST_TEST(v == vec_type({0, 0, 1, 2, 3}));
v.insert(v.begin() + 2, 3, 9);
BOOST_TEST(v == vec_type({0, 0, 9, 9, 9, 1, 2, 3}));
}
{
vec_type v = {1, 2};
static_assert(
std::is_same<
decltype(v.insert(v.begin(), std::initializer_list<int>{0, 0})),
vec_type::iterator>::value,
"");
v.insert(v.begin(), std::initializer_list<int>{0, 0});
BOOST_TEST(v == vec_type({0, 0, 1, 2}));
v.insert(v.end(), std::initializer_list<int>{3});
BOOST_TEST(v == vec_type({0, 0, 1, 2, 3}));
v.insert(v.begin() + 2, std::initializer_list<int>{9, 9, 9});
BOOST_TEST(v == vec_type({0, 0, 9, 9, 9, 1, 2, 3}));
}
}
void test_erase()
{
{
vec_type v = {3, 2, 1};
static_assert(
std::is_same<
decltype(v.erase(v.begin(), v.end())),
vec_type::iterator>::value,
"");
static_assert(
std::is_same<decltype(v.erase(v.begin())), vec_type::iterator>::
value,
"");
v.erase(v.begin(), v.end());
BOOST_TEST(v.empty());
BOOST_TEST(v.size() == 0u);
}
{
vec_type v = {3, 2, 1};
v.erase(v.begin() + 1, v.end());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 1u);
BOOST_TEST(v == vec_type(1, 3));
}
{
vec_type v = {3, 2, 1};
v.erase(v.begin(), v.end() - 1);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 1u);
BOOST_TEST(v == vec_type(1, 1));
}
{
vec_type v = {3, 2, 1};
v.erase(v.begin());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 2u);
BOOST_TEST(v == vec_type({2, 1}));
}
{
vec_type v = {3, 2, 1};
v.erase(v.begin() + 1);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 2u);
BOOST_TEST(v == vec_type({3, 1}));
}
{
vec_type v = {3, 2, 1};
v.erase(v.begin() + 2);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 2u);
BOOST_TEST(v == vec_type({3, 2}));
}
}
template<
typename Container,
typename ValueType = typename Container::value_type>
using lvalue_push_front_t = decltype(
std::declval<Container &>().push_front(std::declval<ValueType const &>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using rvalue_push_front_t = decltype(std::declval<Container &>().push_front(0));
template<typename Container>
using pop_front_t = decltype(std::declval<Container &>().pop_front());
static_assert(ill_formed<lvalue_push_front_t, vec_type>::value, "");
static_assert(ill_formed<rvalue_push_front_t, vec_type>::value, "");
static_assert(ill_formed<pop_front_t, vec_type>::value, "");
void test_front_back()
{
{
vec_type v;
int const i = 0;
static_assert(std::is_same<decltype(v.push_back(0)), void>::value, "");
static_assert(std::is_same<decltype(v.push_back(i)), void>::value, "");
static_assert(std::is_same<decltype(v.pop_back()), void>::value, "");
v.push_back(i);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == i);
v.push_back(1);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 1);
v.push_back(2);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 2);
static_assert(std::is_same<decltype(v.front()), int &>::value, "");
static_assert(std::is_same<decltype(v.back()), int &>::value, "");
v.front() = 9;
v.back() = 8;
BOOST_TEST(v == vec_type({9, 1, 8}));
v.pop_back();
BOOST_TEST(v == vec_type({9, 1}));
}
{
vec_type const v = {3, 2, 1};
BOOST_TEST(v.front() == 3);
BOOST_TEST(v.back() == 1);
static_assert(
std::is_same<decltype(v.front()), int const &>::value, "");
static_assert(std::is_same<decltype(v.back()), int const &>::value, "");
}
}
void test_data_index_at()
{
{
vec_type v = {3, 2, 1};
BOOST_TEST(v.data()[0] == 3);
BOOST_TEST(v.data()[1] == 2);
BOOST_TEST(v.data()[2] == 1);
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
BOOST_TEST(v[2] == 1);
BOOST_TEST_NO_THROW(v.at(0));
BOOST_TEST_NO_THROW(v.at(1));
BOOST_TEST_NO_THROW(v.at(2));
BOOST_TEST_THROWS(v.at(3), std::out_of_range);
static_assert(std::is_same<decltype(v.data()), int *>::value, "");
static_assert(std::is_same<decltype(v[0]), int &>::value, "");
static_assert(std::is_same<decltype(v.at(0)), int &>::value, "");
v[0] = 8;
v.at(1) = 9;
BOOST_TEST(v == vec_type({8, 9, 1}));
}
{
vec_type const v = {3, 2, 1};
BOOST_TEST(v.data()[0] == 3);
BOOST_TEST(v.data()[1] == 2);
BOOST_TEST(v.data()[2] == 1);
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
BOOST_TEST(v[2] == 1);
BOOST_TEST_NO_THROW(v.at(0));
BOOST_TEST_NO_THROW(v.at(1));
BOOST_TEST_NO_THROW(v.at(2));
BOOST_TEST_THROWS(v.at(3), std::out_of_range);
static_assert(std::is_same<decltype(v.data()), int const *>::value, "");
static_assert(std::is_same<decltype(v[0]), int const &>::value, "");
static_assert(std::is_same<decltype(v.at(0)), int const &>::value, "");
}
}
int main()
{
test_default_ctor();
test_other_ctors_assign_ctor();
test_resize();
test_assignment_copy_move_equality();
test_comparisons();
test_swap();
test_iterators();
test_emplace_insert();
test_erase();
test_front_back();
test_data_index_at();
return boost::report_errors();
}

View File

@@ -0,0 +1,921 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#include "../example/static_vector.hpp"
#include "ill_formed.hpp"
#include <boost/core/lightweight_test.hpp>
#include <array>
struct noncopyable_int
{
noncopyable_int() : value_(0) {}
noncopyable_int(noncopyable_int const &) = delete;
noncopyable_int(noncopyable_int && other) : value_(other.value_) {}
noncopyable_int & operator=(noncopyable_int const &) = delete;
noncopyable_int & operator=(noncopyable_int && rhs)
{
value_ = rhs.value_;
return *this;
}
noncopyable_int(int i) : value_(i) {}
noncopyable_int & operator=(int i)
{
value_ = i;
return *this;
}
operator int() const { return value_; }
int value_;
};
void swap(noncopyable_int & x, noncopyable_int & y)
{
std::swap(x.value_, y.value_);
}
#define USE_STD_VECTOR 0
#if USE_STD_VECTOR
using vec_type = std::vector<noncopyable_int>;
#else
using vec_type = static_vector<noncopyable_int, 10>;
#endif
void test_default_ctor()
{
vec_type v;
BOOST_TEST(v.empty());
BOOST_TEST(v.size() == 0u);
#if !USE_STD_VECTOR
BOOST_TEST(v.max_size() == 10u);
BOOST_TEST(v.capacity() == 10u);
#endif
BOOST_TEST(v == v);
BOOST_TEST(v <= v);
BOOST_TEST(v >= v);
BOOST_TEST_THROWS(v.at(0), std::out_of_range);
vec_type const & cv = v;
BOOST_TEST(cv.empty());
BOOST_TEST(cv.size() == 0u);
#if !USE_STD_VECTOR
BOOST_TEST(cv.max_size() == 10u);
BOOST_TEST(cv.capacity() == 10u);
#endif
BOOST_TEST(cv == cv);
BOOST_TEST(cv <= cv);
BOOST_TEST(cv >= cv);
BOOST_TEST_THROWS(cv.at(0), std::out_of_range);
}
void test_other_ctors_assign_ctor()
{
{
vec_type v(3);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
}
#if 0 // initializer_list construction is not supported for move-only value_type.
vec_type v2(std::initializer_list<noncopyable_int>{0, 0, 0});
BOOST_TEST(v == v2);
{
std::initializer_list<noncopyable_int> il{3, 2, 1};
vec_type v(il);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(std::is_same<decltype(v = il), vec_type &>::value, "");
vec_type v2;
v2 = il;
BOOST_TEST(v == v2);
}
{
std::initializer_list<noncopyable_int> il{3, 2, 1};
vec_type v;
v.assign(il);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(std::is_same<decltype(v.assign(il)), void>::value, "");
vec_type v2;
v2 = il;
BOOST_TEST(v == v2);
}
#endif
#if 0 // Code that boils down to fill-insert is not supported for move-only value_type.
{
vec_type v(3, 4);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
vec_type v2 = {4, 4, 4};
BOOST_TEST(v == v2);
}
{
vec_type v;
v.assign(3, 4);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(std::is_same<decltype(v.assign(3, 4)), void>::value, "");
vec_type v2 = {4, 4, 4};
BOOST_TEST(v == v2);
}
#endif
{
std::array<int, 3> a = {{1, 2, 3}};
vec_type v(a.begin(), a.end());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
vec_type v2;
v2.push_back(1);
v2.push_back(2);
v2.push_back(3);
BOOST_TEST(v == v2);
}
{
std::array<int, 3> a = {{1, 2, 3}};
vec_type v;
v.assign(a.begin(), a.end());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 3u);
static_assert(
std::is_same<decltype(v.assign(a.begin(), a.end())), void>::value,
"");
vec_type v2;
v2.push_back(1);
v2.push_back(2);
v2.push_back(3);
BOOST_TEST(v == v2);
}
}
void test_resize()
{
{
vec_type v;
static_assert(std::is_same<decltype(v.resize(1)), void>::value, "");
v.resize(3);
BOOST_TEST(v == vec_type(3));
v.resize(6);
BOOST_TEST(v == vec_type(6));
}
{
vec_type v(6);
v.resize(3);
BOOST_TEST(v == vec_type(3));
v.resize(0);
BOOST_TEST(v == vec_type{});
}
}
void test_assignment_copy_move_equality()
{
{
vec_type v2;
v2.push_back(4);
v2.push_back(4);
v2.push_back(4);
vec_type v(std::move(v2));
BOOST_TEST(v[0] == 4);
BOOST_TEST(v[1] == 4);
BOOST_TEST(v[2] == 4);
BOOST_TEST(v2.empty());
}
{
vec_type v;
vec_type v2;
v2.push_back(4);
v2.push_back(4);
v2.push_back(4);
v = std::move(v2);
BOOST_TEST(v[0] == 4);
BOOST_TEST(v[1] == 4);
BOOST_TEST(v[2] == 4);
BOOST_TEST(v2.empty());
}
}
void test_comparisons()
{
vec_type sm;
sm.push_back(1);
sm.push_back(2);
sm.push_back(3);
vec_type md;
md.push_back(1);
md.push_back(2);
md.push_back(3);
md.push_back(4);
vec_type lg;
lg.push_back(1);
lg.push_back(2);
lg.push_back(3);
lg.push_back(4);
lg.push_back(5);
BOOST_TEST(sm == sm);
BOOST_TEST(!(sm == md));
BOOST_TEST(!(sm == lg));
BOOST_TEST(!(sm != sm));
BOOST_TEST(sm != md);
BOOST_TEST(sm != lg);
BOOST_TEST(!(sm < sm));
BOOST_TEST(sm < md);
BOOST_TEST(sm < lg);
BOOST_TEST(sm <= sm);
BOOST_TEST(sm <= md);
BOOST_TEST(sm <= lg);
BOOST_TEST(!(sm > sm));
BOOST_TEST(!(sm > md));
BOOST_TEST(!(sm > lg));
BOOST_TEST(sm >= sm);
BOOST_TEST(!(sm >= md));
BOOST_TEST(!(sm >= lg));
BOOST_TEST(!(md == sm));
BOOST_TEST(md == md);
BOOST_TEST(!(md == lg));
BOOST_TEST(!(md < sm));
BOOST_TEST(!(md < md));
BOOST_TEST(md < lg);
BOOST_TEST(!(md <= sm));
BOOST_TEST(md <= md);
BOOST_TEST(md <= lg);
BOOST_TEST(md > sm);
BOOST_TEST(!(md > md));
BOOST_TEST(!(md > lg));
BOOST_TEST(md >= sm);
BOOST_TEST(md >= md);
BOOST_TEST(!(md >= lg));
BOOST_TEST(!(lg == sm));
BOOST_TEST(!(lg == md));
BOOST_TEST(lg == lg);
BOOST_TEST(!(lg < sm));
BOOST_TEST(!(lg < md));
BOOST_TEST(!(lg < lg));
BOOST_TEST(!(lg <= sm));
BOOST_TEST(!(lg <= md));
BOOST_TEST(lg <= lg);
BOOST_TEST(lg > sm);
BOOST_TEST(lg > md);
BOOST_TEST(!(lg > lg));
BOOST_TEST(lg >= sm);
BOOST_TEST(lg >= md);
BOOST_TEST(lg >= lg);
}
void test_swap()
{
{
vec_type v1;
v1.push_back(4);
v1.push_back(4);
v1.push_back(4);
vec_type v2;
v2.push_back(3);
v2.push_back(3);
v2.push_back(3);
v2.push_back(3);
static_assert(std::is_same<decltype(v1.swap(v2)), void>::value, "");
static_assert(std::is_same<decltype(swap(v1, v2)), void>::value, "");
v1.swap(v2);
BOOST_TEST(v1.size() == 4u);
BOOST_TEST(v2.size() == 3u);
BOOST_TEST(v1[0] == 3);
BOOST_TEST(v2[0] == 4);
}
{
vec_type v1;
v1.push_back(4);
v1.push_back(4);
v1.push_back(4);
vec_type v2;
v2.push_back(3);
v2.push_back(3);
v2.push_back(3);
v2.push_back(3);
swap(v1, v2);
BOOST_TEST(v1.size() == 4u);
BOOST_TEST(v2.size() == 3u);
BOOST_TEST(v1[0] == 3);
BOOST_TEST(v2[0] == 4);
}
}
template<typename Iter>
using writable_iter_t = decltype(
*std::declval<Iter>() =
std::declval<typename std::iterator_traits<Iter>::value_type>());
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().begin())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().end())>::value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<vec_type &>().cbegin())>::
value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<vec_type &>().cend())>::
value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().rbegin())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type const &>().rend())>::value,
"");
static_assert(
ill_formed<
writable_iter_t,
decltype(std::declval<vec_type &>().crbegin())>::value,
"");
static_assert(
ill_formed<writable_iter_t, decltype(std::declval<vec_type &>().crend())>::
value,
"");
void test_iterators()
{
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
static_assert(
std::is_same<decltype(v.begin()), vec_type::iterator>::value, "");
static_assert(
std::is_same<decltype(v.end()), vec_type::iterator>::value, "");
static_assert(
std::is_same<decltype(v.cbegin()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cend()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.rbegin()), vec_type::reverse_iterator>::
value,
"");
static_assert(
std::is_same<decltype(v.rbegin()), vec_type::reverse_iterator>::
value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
std::array<int, 3> const a = {{3, 2, 1}};
std::array<int, 3> const ra = {{1, 2, 3}};
BOOST_TEST(std::equal(v.begin(), v.end(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.cbegin(), v.cend(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.rbegin(), v.rend(), ra.begin(), ra.end()));
BOOST_TEST(std::equal(v.crbegin(), v.crend(), ra.begin(), ra.end()));
*v.begin() = 8;
*v.rbegin() = 9;
BOOST_TEST(v[0] == 8);
BOOST_TEST(v[2] == 9);
}
{
vec_type v_;
v_.push_back(3);
v_.push_back(2);
v_.push_back(1);
vec_type const & v = v_;
static_assert(
std::is_same<decltype(v.begin()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.end()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cbegin()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<decltype(v.cend()), vec_type::const_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.rbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.rbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.crbegin()),
vec_type::const_reverse_iterator>::value,
"");
std::array<int, 3> const a = {{3, 2, 1}};
std::array<int, 3> const ra = {{1, 2, 3}};
BOOST_TEST(std::equal(v.begin(), v.end(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.cbegin(), v.cend(), a.begin(), a.end()));
BOOST_TEST(std::equal(v.rbegin(), v.rend(), ra.begin(), ra.end()));
BOOST_TEST(std::equal(v.crbegin(), v.crend(), ra.begin(), ra.end()));
}
}
void test_emplace_insert()
{
{
vec_type v;
int const i = 0;
static_assert(
std::is_same<decltype(v.emplace_back(0)), vec_type::reference>::
value,
"");
static_assert(
std::is_same<decltype(v.emplace_back(i)), vec_type::reference>::
value,
"");
v.emplace_back(i);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == i);
v.emplace_back(1);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 1);
v.emplace_back(2);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 2);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 2);
}
{
vec_type v;
v.push_back(1);
v.push_back(2);
int const i = 0;
static_assert(
std::is_same<
decltype(v.emplace(v.begin(), 0)),
vec_type::iterator>::value,
"");
static_assert(
std::is_same<
decltype(v.emplace(v.begin(), i)),
vec_type::iterator>::value,
"");
v.emplace(v.begin(), i);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 2);
v.emplace(v.end(), 3);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 2);
BOOST_TEST(v[3] == 3);
v.emplace(v.begin() + 2, 9);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 9);
BOOST_TEST(v[3] == 2);
BOOST_TEST(v[4] == 3);
}
{
vec_type v;
v.push_back(1);
v.push_back(2);
std::array<int, 2> a1 = {{0, 0}};
std::array<int, 1> a2 = {{3}};
std::array<int, 3> a3 = {{9, 9, 9}};
static_assert(
std::is_same<
decltype(v.insert(v.begin(), a1.begin(), a1.end())),
vec_type::iterator>::value,
"");
auto const it0 = v.insert(v.begin(), a1.begin(), a1.end());
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 0);
BOOST_TEST(v[2] == 1);
BOOST_TEST(v[3] == 2);
BOOST_TEST(it0 == v.begin());
auto const it1 = v.insert(v.end(), a2.begin(), a2.end());
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 0);
BOOST_TEST(v[2] == 1);
BOOST_TEST(v[3] == 2);
BOOST_TEST(v[4] == 3);
BOOST_TEST(it1 == v.begin() + 4);
auto const it2 = v.insert(v.begin() + 2, a3.begin(), a3.end());
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 0);
BOOST_TEST(v[2] == 9);
BOOST_TEST(v[3] == 9);
BOOST_TEST(v[4] == 9);
BOOST_TEST(v[5] == 1);
BOOST_TEST(v[6] == 2);
BOOST_TEST(v[7] == 3);
BOOST_TEST(it2 == v.begin() + 2);
}
{
vec_type v;
v.push_back(1);
v.push_back(2);
int const i = 0;
static_assert(
std::is_same<decltype(v.insert(v.begin(), 0)), vec_type::iterator>::
value,
"");
static_assert(
std::is_same<decltype(v.insert(v.begin(), i)), vec_type::iterator>::
value,
"");
v.emplace(v.begin(), i);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 2);
v.emplace(v.end(), 3);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 2);
BOOST_TEST(v[3] == 3);
v.emplace(v.begin() + 2, 9);
BOOST_TEST(v[0] == 0);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 9);
BOOST_TEST(v[3] == 2);
BOOST_TEST(v[4] == 3);
}
#if 0 // Fill-insert is not supported for move-only value_type.
{
vec_type v;
v.push_back(1);
v.push_back(2);
static_assert(
std::is_same<
decltype(v.insert(v.begin(), 3, 0)),
vec_type::iterator>::value,
"");
v.insert(v.begin(), 2, 0);
v.insert(v.end(), 1, 3);
v.insert(v.begin() + 2, 3, 9);
}
#endif
#if 0 // initializer_list-insert is not supported for move-only value_type.
{
vec_type v;
v.push_back(1);
v.push_back(2);
static_assert(
std::is_same<
decltype(v.insert(v.begin(), std::initializer_list<noncopyable_int>{0, 0})),
vec_type::iterator>::value,
"");
v.insert(v.begin(), std::initializer_list<noncopyable_int>{0, 0});
v.insert(v.end(), std::initializer_list<noncopyable_int>{3});
v.insert(v.begin() + 2, std::initializer_list<noncopyable_int>{9, 9, 9});
}
#endif
}
void test_erase()
{
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
static_assert(
std::is_same<
decltype(v.erase(v.begin(), v.end())),
vec_type::iterator>::value,
"");
static_assert(
std::is_same<decltype(v.erase(v.begin())), vec_type::iterator>::
value,
"");
v.erase(v.begin(), v.end());
BOOST_TEST(v.empty());
BOOST_TEST(v.size() == 0u);
}
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
v.erase(v.begin() + 1, v.end());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 1u);
BOOST_TEST(v[0] == 3);
}
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
v.erase(v.begin(), v.end() - 1);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 1u);
BOOST_TEST(v[0] == 1);
}
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
v.erase(v.begin());
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 2u);
BOOST_TEST(v[0] == 2);
BOOST_TEST(v[1] == 1);
}
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
v.erase(v.begin() + 1);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 2u);
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 1);
}
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
v.erase(v.begin() + 2);
BOOST_TEST(!v.empty());
BOOST_TEST(v.size() == 2u);
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
}
}
template<
typename Container,
typename ValueType = typename Container::value_type>
using lvalue_push_front_t = decltype(
std::declval<Container &>().push_front(std::declval<ValueType const &>()));
template<
typename Container,
typename ValueType = typename Container::value_type>
using rvalue_push_front_t = decltype(std::declval<Container &>().push_front(0));
template<typename Container>
using pop_front_t = decltype(std::declval<Container &>().pop_front());
static_assert(ill_formed<lvalue_push_front_t, vec_type>::value, "");
static_assert(ill_formed<rvalue_push_front_t, vec_type>::value, "");
static_assert(ill_formed<pop_front_t, vec_type>::value, "");
void test_front_back()
{
{
vec_type v;
int const i = 0;
static_assert(std::is_same<decltype(v.push_back(0)), void>::value, "");
static_assert(std::is_same<decltype(v.push_back(i)), void>::value, "");
static_assert(std::is_same<decltype(v.pop_back()), void>::value, "");
v.emplace_back(i);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == i);
v.emplace_back(1);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 1);
v.emplace_back(2);
BOOST_TEST(v.front() == i);
BOOST_TEST(v.back() == 2);
static_assert(std::is_same<decltype(v.front()), noncopyable_int &>::value, "");
static_assert(std::is_same<decltype(v.back()), noncopyable_int &>::value, "");
v.front() = 9;
v.back() = 8;
BOOST_TEST(v[0] == 9);
BOOST_TEST(v[1] == 1);
BOOST_TEST(v[2] == 8);
v.pop_back();
BOOST_TEST(v[0] == 9);
BOOST_TEST(v[1] == 1);
}
{
vec_type v_;
v_.push_back(3);
v_.push_back(2);
v_.push_back(1);
vec_type const & v = v_;
BOOST_TEST(v.front() == 3);
BOOST_TEST(v.back() == 1);
static_assert(
std::is_same<decltype(v.front()), noncopyable_int const &>::value, "");
static_assert(std::is_same<decltype(v.back()), noncopyable_int const &>::value, "");
}
}
void test_data_index_at()
{
{
vec_type v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
BOOST_TEST(v.data()[0] == 3);
BOOST_TEST(v.data()[1] == 2);
BOOST_TEST(v.data()[2] == 1);
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
BOOST_TEST(v[2] == 1);
BOOST_TEST_NO_THROW(v.at(0));
BOOST_TEST_NO_THROW(v.at(1));
BOOST_TEST_NO_THROW(v.at(2));
BOOST_TEST_THROWS(v.at(3), std::out_of_range);
static_assert(std::is_same<decltype(v.data()), noncopyable_int *>::value, "");
static_assert(std::is_same<decltype(v[0]), noncopyable_int &>::value, "");
static_assert(std::is_same<decltype(v.at(0)), noncopyable_int &>::value, "");
v[0] = 8;
v.at(1) = 9;
BOOST_TEST(v[0] == 8);
BOOST_TEST(v[1] == 9);
BOOST_TEST(v[2] == 1);
}
{
vec_type v_;
v_.push_back(3);
v_.push_back(2);
v_.push_back(1);
vec_type const & v = v_;
BOOST_TEST(v.data()[0] == 3);
BOOST_TEST(v.data()[1] == 2);
BOOST_TEST(v.data()[2] == 1);
BOOST_TEST(v[0] == 3);
BOOST_TEST(v[1] == 2);
BOOST_TEST(v[2] == 1);
BOOST_TEST_NO_THROW(v.at(0));
BOOST_TEST_NO_THROW(v.at(1));
BOOST_TEST_NO_THROW(v.at(2));
BOOST_TEST_THROWS(v.at(3), std::out_of_range);
static_assert(std::is_same<decltype(v.data()), noncopyable_int const *>::value, "");
static_assert(std::is_same<decltype(v[0]), noncopyable_int const &>::value, "");
static_assert(std::is_same<decltype(v.at(0)), noncopyable_int const &>::value, "");
}
}
int main()
{
test_default_ctor();
test_other_ctors_assign_ctor();
test_resize();
test_assignment_copy_move_equality();
test_comparisons();
test_swap();
test_iterators();
test_emplace_insert();
test_erase();
test_front_back();
test_data_index_at();
return boost::report_errors();
}

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2019 T. Zachary Laine
//
// 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)
#ifndef BOOST_STL_INTERFACES_VIEW_TESTING_HPP
#define BOOST_STL_INTERFACES_VIEW_TESTING_HPP
#include <boost/stl_interfaces/view_interface.hpp>
template<
typename Iterator,
typename Sentinel,
boost::stl_interfaces::element_layout Contiguity>
struct subrange
: boost::stl_interfaces::
view_interface<subrange<Iterator, Sentinel, Contiguity>, Contiguity>
{
subrange() = default;
constexpr subrange(Iterator it, Sentinel s) : first_(it), last_(s) {}
constexpr auto begin() const { return first_; }
constexpr auto end() const { return last_; }
private:
Iterator first_;
Sentinel last_;
};
template<
boost::stl_interfaces::element_layout Contiguity,
typename Iterator,
typename Sentinel>
auto range(Iterator i, Sentinel s)
{
return subrange<Iterator, Sentinel, Contiguity>(i, s);
}
#endif