More concepts
#include <more_concepts/more_concepts.hpp>
This library aims to provide general purpose concepts that are not available in the C++20 concepts library, most notably container concepts. It also provides utilities for writing your own concepts (a mock_iterator
that can mock any iterator category - see bellow).
Notes
At the moment, only GCC 10.2 is confirmed to be able to compile all of this library.
Installing with Conan
If you use Conan to manage dependencies, you can get this library from my artifactory.
- Add my artifactory repository to your local Conan install:
conan remote add miso1289 https://miso1289.jfrog.io/artifactory/api/conan/miso1289
- Add the reference to your
conanfile.txt
(orconanfile.py
), e.g.more_concepts/[email protected]/stable
- Install the dependency before building:
conan install -b missing $CONAN_DIR
, whereCONAN_DIR
is your top-level directory (containing your conanfile).
Container concepts
The container concepts are intended to serve as an abstraction for the STL container interfaces, allowing writing constrained generic code that can use any container of some category, both standard and third-party (as long as it provides an STL compliant interface).
While a lot of generic algorithms can do with range and iterator concepts, this is sometimes not enough - for example, how does one write a constrained container adaptor? Let's say you want to implement a priority queue that can use any index-able sequence type with back-insertion to store the heap. Using this library, you could express this as:
template <typename T,
more_concepts::random_access_container_of Seq = std::vector>
requires more_concepts::back_growable_container
class priority_queue;
General container concepts
#include <more_concepts/base_containers.hpp>
container
- Satisfied by all well-behaved (notvector
) standard containers.mutable_container
- A container that allows mutable iteration. Satisfied by all standard containers exceptset
.sized_container
- A container that knows its size. Satisfied by all standard containers exceptforward_list
.clearable_container
- A container that can be cleared . Satisfied by all standard containers exceptarray
.reversible_container
- A container that allows reverse iteration. Satisfied by all standard containers exceptforward_list
.
For each of the above, an _of
version is provided (e.g. reversible_container_of
), that also requires the value_type
to be the same as specified.
Sequence container concepts
#include <more_concepts/sequence_containers.hpp>
sequence_container
- A mutable container that represents linear ordering of elements (corresponds to the standardSequenceContainer
named requirement). Provides efficient access to the beginning of the sequence. Satisfied by all standard sequence containers, namelyvector
,array
,basic_string
,deque
,list
, andforward_list
.double_ended_container
- A sized and reversible sequence container that provides efficient access to the end of the sequence. Satisfied by all standard sequence containers exceptforward_list
.random_access_container
- A double-ended sequence container that provides indexed access to elements. Satisfied byvector
,array
,basic_string
, anddeque
.contiguous_container
- A random-access sequence container, stored contiguously in memory. Satisfied byvector
,array
, andbasic_string
.resizable_sequence_container
- A clearable double-ended container that allows resizing, range construction and assignment, and insertion / erasure in the middle). Satisfied byvector
,basic_string
,deque
, andlist
.inplace_constructing_sequence_container
- Extends theresizable_sequence_container
interface with in-place construction. Satisfied by all standard models ofresizable_sequence_container
exceptbasic_string
.front_growable_container
- A sequence container that allows efficient inserting / erasure at the front. Satisfied bydeque
,list
, andforward_list
.inplace_front_constructing_container
- Extends thefront_growable_container
interface with in-place construction. Satisfied by all standard models offront_growable_container
.back_growable_container
- A double-ended container that allows efficient inserting / erasure at the back. Satisfied byvector
,basic_string
,deque
, andlist
.inplace_back_constructing_container
- Extends theback_growable_container
interface with in-place construction. Satisfied by all standard models ofback_growable_container
exceptbasic_string
.
For each sequence container concept, an _of
version is also provided.
Associative container concepts
#include <more_concepts/associative_containers.hpp>
associative_container
- A container that provides fast lookup of objects based on keys. Represents a union of the standard named requirementsAssociativeContainer
andUnorderedAssociativeContainer
. Satisfied by all standard associative containers.unique_associative_container
- An associative container with unique keys. Satisfied byset
,map
,unordered_set
andunordered_map
.multiple_associative_container
- An associative container with non-unique keys. Satisfied bymultiset
,multimap
,unordered_multiset
andunordered_multimap
.map_container
- An associative container representing a key-value mapping. Satisfied bymap
,multimap
,unordered_map
andunordered_multimap
.unique_map_container
- A map container with unique keys. Satisfied bymap
andunordered_map
.multiple_map_container
- A map container with non-unique keys. Satisfied bymultimap
andunordered_multimap
.
For each associative container / map concept, ordered_
and unordered_
versions are available (e.g. ordered_unique_map_container
).
For each generic (non-map) associative container concept, an _of
version is available.
For each map container concept, an _of
version is available.
General concepts
#include <more_concepts/base_concepts.hpp>
Concepts that are simple wrappers over standard type traits:
decayed
- Types that are non-reference, non-c-array, non-function or function reference, non-const and non-volatile. Assigning an object of this type to an auto variable preserves the type. Used to constrain thevalue_type
of containers.aggregate
- Types that support aggregate initialization.trivial
- Types that can bememcpy
-ied, and don't need any (non-trivial) initialization or destruction.enum_type
- Scoped and unscoped enumeration types.error_code_enum
- Error enum that can be used to construct astd::error_code
.error_condition_enum
- Error enum that can be used to construct astd::error_condition
.
Function concepts:
invocable_as
- Function types that can be called withstd::invoke
using one or more function signatures. The return type of each signature is only checked for convertibility.callable_as
- Function types that can be called with the function-call operator using one or more function signatures. The return type of each signature must be matched exactly.hash_function
- corresponds to theHash
standard named requirement. Used to define theunordered_associative_container
concept.
Utilities
Mock iterator
#include <more_concepts/mock_iterator.hpp>
The mock_iterator
class template can be used to write concepts that require some operation to accept any iterator of some category.
Template parameters:
T
- iterator value type.IteratorCategory
- can be one of the standard iterator category tags (e.g.std::input_iterator_tag
). The mock iterator provides the minimal needed interface to satisfy the requested category. E.g. for the input and output iterator categories, a proxy reference type is used instead of a raw reference.RWCategory
- can be one ofmutable_iterator_tag
,const_iterator_tag
. Indicates whether the mock iterator should support write access.