BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_stringviewlikeparam.h
Go to the documentation of this file.
1
/// @file bslstl_stringviewlikeparam.h
2
///
3
/// The content of this file has been pre-processed for Doxygen.
4
///
5
6
7
// bslstl_stringviewlikeparam.h -*-C++-*-
8
#ifndef INCLUDED_BSLSTL_STRINGVIEWLIKEPARAM
9
#define INCLUDED_BSLSTL_STRINGVIEWLIKEPARAM
10
11
#include <
bsls_ident.h
>
12
BSLS_IDENT
(
"$Id: $"
)
13
14
/// @defgroup bslstl_stringviewlikeparam bslstl_stringviewlikeparam
15
/// @brief Provide macros for SFINAE constraints on string-view-like params.
16
/// @addtogroup bsl
17
/// @{
18
/// @addtogroup bslstl
19
/// @{
20
/// @addtogroup bslstl_stringviewlikeparam
21
/// @{
22
///
23
/// <h1> Outline </h1>
24
/// * <a href="#bslstl_stringviewlikeparam-purpose"> Purpose</a>
25
/// * <a href="#bslstl_stringviewlikeparam-macros"> Macros </a>
26
/// * <a href="#bslstl_stringviewlikeparam-description"> Description </a>
27
/// * <a href="#bslstl_stringviewlikeparam-macro-summary"> Macro Summary </a>
28
/// * <a href="#bslstl_stringviewlikeparam-usage"> Usage </a>
29
/// * <a href="#bslstl_stringviewlikeparam-example-1-declaring-a-string-view-like-constructor"> Example 1: Declaring a String-View-Like Constructor </a>
30
///
31
/// # Purpose {#bslstl_stringviewlikeparam-purpose}
32
/// Provide macros for SFINAE constraints on string-view-like params.
33
///
34
/// # Macros {#bslstl_stringviewlikeparam-macros}
35
///
36
/// - BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE: completeness check
37
/// - BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T: SV-convertible enable_if
38
/// - BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T: SV-conv, not C-string
39
///
40
/// @see bslstl_isconvertibletostringview, bslstl_isconvertibletocstring
41
///
42
/// # Description {#bslstl_stringviewlikeparam-description}
43
/// This component provides a set of macros that simplify the
44
/// declaration of function template parameters constrained to accept
45
/// "string-view-like" types. A type is considered "string-view-like" if it is
46
/// convertible to `bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>`.
47
///
48
/// The "ONLY" variants additionally exclude types that are convertible to
49
/// `const CHAR_TYPE *` (C-strings), which is useful for overload resolution
50
/// when separate overloads exist for C-string arguments.
51
///
52
/// These macros are designed to be used in function declarations and
53
/// definitions where SFINAE is needed to constrain template parameters.
54
///
55
/// ## Macro Summary {#bslstl_stringviewlikeparam-macro-summary}
56
///
57
///
58
/// This section provides a brief description of each macro:
59
///
60
/// - `BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE`: Helper macro that
61
/// provides a completeness check for the `STRING_VIEW_LIKE_TYPE` template
62
/// parameter, working around SFINAE deficiencies in certain compilers.
63
///
64
/// - `BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(ResultType)`: SFINAE
65
/// `enable_if` type alias for types convertible to string_view. The result
66
/// is `ResultType` if enabled. Use as:
67
/// `BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(void) * = 0` for declaration,
68
/// or `BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(const ALLOCATOR&) alloc =`
69
/// `ALLOCATOR()`
70
///
71
/// - `BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(ResultType)`: SFINAE
72
/// `enable_if` type alias for types convertible to string_view but NOT
73
/// convertible to C-string (`const CHAR_TYPE *`). The result is
74
/// `ResultType` if enabled. Use as:
75
/// `BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(void) * = 0` for
76
/// declaration, or
77
/// `BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(const ALLOCATOR&) alloc =`
78
/// `ALLOCATOR()`
79
///
80
/// ## Usage {#bslstl_stringviewlikeparam-usage}
81
///
82
///
83
/// This section illustrates intended use of this component.
84
///
85
/// ### Example 1: Declaring a String-View-Like Constructor {#bslstl_stringviewlikeparam-example-1-declaring-a-string-view-like-constructor}
86
///
87
///
88
/// Suppose we want to declare a constructor that accepts any type convertible
89
/// to `bsl::string_view` but not `const char *`.
90
///
91
/// First, we declare the constructor in the class definition:
92
/// @code
93
/// template <class STRING_VIEW_LIKE_TYPE>
94
/// explicit MyClass(const STRING_VIEW_LIKE_TYPE& value,
95
/// BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(void) *= 0);
96
/// @endcode
97
/// Then, we define the constructor outside the class:
98
/// @code
99
/// template <class STRING_VIEW_LIKE_TYPE>
100
/// MyClass::MyClass(const STRING_VIEW_LIKE_TYPE& value,
101
/// BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(void) *)
102
/// {
103
/// bsl::string_view sv = value;
104
/// // use sv...
105
/// }
106
/// @endcode
107
/// @}
108
/** @} */
109
/** @} */
110
111
/** @addtogroup bsl
112
* @{
113
*/
114
/** @addtogroup bslstl
115
* @{
116
*/
117
/** @addtogroup bslstl_stringviewlikeparam
118
* @{
119
*/
120
121
#include <bslscm_version.h>
122
123
#include <
bslstl_isconvertibletocstring.h
>
124
#include <
bslstl_isconvertibletostringview.h
>
125
126
#include <
bslmf_enableif.h
>
127
128
#include <
bsls_platform.h
>
129
130
/// We need to use an intermediate completeness test to work around
131
/// deficiencies with SFINAE in the Sun and AIX compilers.
132
#define BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE \
133
typename bsl::enable_if<0 != sizeof(STRING_VIEW_LIKE_TYPE), \
134
const STRING_VIEW_LIKE_TYPE&>::type
135
136
/// Expand to an `enable_if` type alias for `ResultType` that is defined
137
/// (i.e., SFINAE-friendly) only when the `STRING_VIEW_LIKE_TYPE` template
138
/// parameter is convertible to
139
/// `bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>`. The `CHAR_TYPE` and
140
/// `CHAR_TRAITS` template parameters must be in scope. The specified
141
/// `ResultType` (passed as a variadic argument) is the resulting type when
142
/// the condition is satisfied.
143
#define BSLSTL_STRINGVIEWLIKEPARAM_ENABLE_IF_T(...) \
144
typename bsl::enable_if< \
145
BloombergLP::bslstl::IsConvertibleToStringView< \
146
CHAR_TYPE, \
147
CHAR_TRAITS, \
148
BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE>::value, \
149
__VA_ARGS__>::type
150
151
/// Expand to an `enable_if` type alias for `ResultType` that is defined
152
/// (i.e., SFINAE-friendly) only when the `STRING_VIEW_LIKE_TYPE` template
153
/// parameter is convertible to
154
/// `bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>` but is *not*
155
/// convertible to `const CHAR_TYPE *`. The `CHAR_TYPE` and `CHAR_TRAITS`
156
/// template parameters must be in scope. The specified `ResultType`
157
/// (passed as a variadic argument) is the resulting type when both
158
/// conditions are satisfied.
159
#define BSLSTL_STRINGVIEWLIKEPARAM_ONLY_ENABLE_IF_T(...) \
160
typename bsl::enable_if< \
161
BloombergLP::bslstl::IsConvertibleToStringView< \
162
CHAR_TYPE, \
163
CHAR_TRAITS, \
164
BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE>::value \
165
&& !BloombergLP::bslstl::IsConvertibleToCString< \
166
CHAR_TYPE, \
167
BSLSTL_STRINGVIEWLIKEPARAM_TYPE_IF_COMPLETE>::value, \
168
__VA_ARGS__>::type
169
170
#endif
171
172
// ----------------------------------------------------------------------------
173
// Copyright 2026 Bloomberg Finance L.P.
174
//
175
// Licensed under the Apache License, Version 2.0 (the "License");
176
// you may not use this file except in compliance with the License.
177
// You may obtain a copy of the License at
178
//
179
// http://www.apache.org/licenses/LICENSE-2.0
180
//
181
// Unless required by applicable law or agreed to in writing, software
182
// distributed under the License is distributed on an "AS IS" BASIS,
183
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184
// See the License for the specific language governing permissions and
185
// limitations under the License.
186
// ----------------------------- END-OF-FILE ----------------------------------
187
188
/** @} */
189
/** @} */
190
/** @} */
bslmf_enableif.h
bsls_ident.h
bsls_platform.h
bslstl_isconvertibletocstring.h
bslstl_isconvertibletostringview.h
BSLS_IDENT
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition
bsls_ident.h:238
doxygen_input
bde
groups
bsl
bslstl
bslstl_stringviewlikeparam.h
Generated by
1.9.8