BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_containercompatiblerange.h
Go to the documentation of this file.
1/// @file bslmf_containercompatiblerange.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_containercompatiblerange.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_CONTAINERCOMPATIBLERANGE
9#define INCLUDED_BSLMF_CONTAINERCOMPATIBLERANGE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_containercompatiblerange bslmf_containercompatiblerange
15/// @brief Provide a concept for the Standard `container-compatible-range`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_containercompatiblerange
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_containercompatiblerange-purpose"> Purpose</a>
25/// * <a href="#bslmf_containercompatiblerange-classes"> Classes </a>
26/// * <a href="#bslmf_containercompatiblerange-description"> Description </a>
27/// * <a href="#bslmf_containercompatiblerange-usage"> Usage </a>
28/// * <a href="#bslmf_containercompatiblerange-example-1-declaring-a-range-compatible-interface"> Example 1: Declaring a Range Compatible Interface </a>
29///
30/// # Purpose {#bslmf_containercompatiblerange-purpose}
31/// Provide a concept for the Standard `container-compatible-range`.
32///
33/// # Classes {#bslmf_containercompatiblerange-classes}
34///
35/// - bslmf::ContainerCompatibleRange: the `container-compatible-range` concept
36///
37/// @see
38///
39/// # Description {#bslmf_containercompatiblerange-description}
40/// This component provides a concept,
41/// `bslmf::ContainerCompatibleRange`, that specifies the requirements for
42/// range-types used to create/set containers. This is a concrete concept
43/// corresponding the Standard, exposition-only, concept
44/// `container-compatible-range`.
45///
46/// ## Usage {#bslmf_containercompatiblerange-usage}
47///
48///
49/// This section illustrates intended use of this component.
50///
51/// ### Example 1: Declaring a Range Compatible Interface {#bslmf_containercompatiblerange-example-1-declaring-a-range-compatible-interface}
52///
53///
54/// Suppose we have a container class that we want to be able to accept content
55/// from a range. We can use `bslmf::ContainerCompatibleRange` concept to
56/// constrain the relevant templates, removing them from the overload set unless
57/// the (template argument) `t_RANGE` meets the required constraints.
58/// @code
59/// // =================
60/// // class MyContainer
61/// // =================
62///
63/// template <class t_TYPE>
64/// class MyContainer {
65///
66/// public:
67/// // CREATORS
68///
69/// // ...
70///
71/// template <bslmf::ContainerCompatibleRange<t_TYPE> t_RANGE>
72/// MyContainer(bsl::from_range_t , t_RANGE&& range);
73///
74/// // ...
75///
76/// // MANIPULATORS
77///
78/// // ...
79/// template <bslmf::ContainerCompatibleRange<t_TYPE> t_RANGE>
80/// MyContainer& append_range(t_RANGE&& range);
81///
82/// // ...
83///
84/// };
85/// @endcode
86/// Notice that the (singular) tag value `bsl::from_range` is used to clearly
87/// direct overload resolution to the range constructor when that is the intent.
88/// For other methods, we have the option to incorporate "range" into the method
89/// name to avoid ambiguous overloads -- i.e., @ref append_range is clearly
90/// different from assorted overloads of `append` (not shown).
91///
92/// Finally, notice that the constraint on `t_RANGE` and `t_TYPE` must be
93/// repeated in the method definitions:
94/// @code
95/// // -----------------
96/// // class MyContainer
97/// // -----------------
98///
99/// // CREATORS
100///
101/// // ...
102///
103/// template <class t_TYPE>
104/// template <bslmf::ContainerCompatibleRange<t_TYPE> t_RANGE>
105/// MyContainer<t_TYPE>::MyContainer(bsl::from_range_t , t_RANGE&& range)
106/// {
107/// // ...
108/// }
109///
110/// // MANIPULATORS
111///
112/// // ...
113///
114/// template <class t_TYPE>
115/// template <bslmf::ContainerCompatibleRange<t_TYPE> t_RANGE>
116/// MyContainer<t_TYPE>& MyContainer<t_TYPE>::append_range(t_RANGE&& range)
117/// {
118/// // ...
119/// return *this;
120/// }
121///
122/// int main()
123/// {
124/// int intArray[] = { 7, 13 };
125/// MyContainer<int> mc(bsl::from_range, intArray);
126/// // OK, `intArray` is a range.
127///
128/// std::vector<int> iVector = { 42, 666 };
129/// mc.append_range(iVector); // OK, `iVector` is also a range.
130///
131/// std::vector<double> dVector = { 1.0, 32.2, 211.95 };
132/// mc.append_range(dVector); // OK, `dVector` is also a range *and*
133/// // `double` is convertible to `int`.
134///
135/// std::vector<std::string> sVector = { "Tom", "Dick", "Harry" };
136/// mc.append_range(sVector); // Error, `sVector` is a range but
137/// // `bsl::string` is *not* implicitly
138/// // convertible to `int`.
139///
140/// int intValue = -1;
141/// mc.append_range(intValue); // Error, `intValue` has expected type
142/// // *but* is not a range.
143///
144/// return 0;
145/// }
146/// @endcode
147/// @}
148/** @} */
149/** @} */
150
151/** @addtogroup bsl
152 * @{
153 */
154/** @addtogroup bslmf
155 * @{
156 */
157/** @addtogroup bslmf_containercompatiblerange
158 * @{
159 */
160
161#include <bslscm_version.h>
162
163#include <bsls_libraryfeatures.h>
164
165#if defined(BSLS_LIBRARYFEATURES_HAS_CPP20_CONCEPTS) \
166 && defined(BSLS_LIBRARYFEATURES_HAS_CPP20_RANGES)
167
168#include <concepts>
169#include <ranges>
170
171
172namespace bslmf {
173
174/// Require that (template parameter) `t_RANGE` meets the requirements of
175/// input range and that the values referenced by the iterators of that
176/// range type have the same type or are convertible to the (template
177/// parameter) `t_TYPE`.
178template <typename t_RANGE, typename t_TYPE>
179concept ContainerCompatibleRange =
180 std::ranges::input_range<t_RANGE>
181 && std::convertible_to<std::ranges::range_reference_t<t_RANGE>, t_TYPE>;
182
183} // close package namespace
184
185#endif
186#endif
187
188// ----------------------------------------------------------------------------
189// Copyright 2025 Bloomberg Finance L.P.
190//
191// Licensed under the Apache License, Version 2.0 (the "License");
192// you may not use this file except in compliance with the License.
193// You may obtain a copy of the License at
194//
195// http://www.apache.org/licenses/LICENSE-2.0
196//
197// Unless required by applicable law or agreed to in writing, software
198// distributed under the License is distributed on an "AS IS" BASIS,
199// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200// See the License for the specific language governing permissions and
201// limitations under the License.
202// ----------------------------- END-OF-FILE ----------------------------------
203
204/** @} */
205/** @} */
206/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlbb_blob.h:579