BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_isconvertibletostringview.h
Go to the documentation of this file.
1/// @file bslstl_isconvertibletostringview.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_isconvertibletostringview.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_ISCONVERTIBLETOSTRINGVIEW
9#define INCLUDED_BSLSTL_ISCONVERTIBLETOSTRINGVIEW
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_isconvertibletostringview bslstl_isconvertibletostringview
15/// @brief Provide a compile-time check for types convertible to string_view.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_isconvertibletostringview
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_isconvertibletostringview-purpose"> Purpose</a>
25/// * <a href="#bslstl_isconvertibletostringview-classes"> Classes </a>
26/// * <a href="#bslstl_isconvertibletostringview-description"> Description </a>
27/// * <a href="#bslstl_isconvertibletostringview-usage"> Usage </a>
28/// * <a href="#bslstl_isconvertibletostringview-example-1-determine-if-a-type-is-convertible-to-a-string-view"> Example 1: Determine If a Type Is Convertible to a String View </a>
29///
30/// # Purpose {#bslstl_isconvertibletostringview-purpose}
31/// Provide a compile-time check for types convertible to string_view.
32///
33/// # Classes {#bslstl_isconvertibletostringview-classes}
34///
35/// - bslstl::IsConvertibleToStringView: meta-function for string_view conversion
36///
37/// @see bslmf_isconvertible, bslstl_isconvertibletocstring,
38/// bslstl_stringview
39///
40/// # Description {#bslstl_isconvertibletostringview-description}
41/// This component provides a meta-function,
42/// `bslstl::IsConvertibleToStringView`, that may be used to query (at
43/// compile-time) whether a type is convertible to a
44/// `bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>`.
45///
46/// `bslstl::IsConvertibleToStringView` meets the requirements of the
47/// `UnaryTypeTrait` concept defined in the C++ standard [meta.rqmts] with a
48/// base characteristic of `bsl::true_type` if the (template parameter) `TYPE`
49/// is convertible to `bsl::basic_string_view<CHAR_TYPE, CHAR_TRAITS>`, and a
50/// base characteristic of `bsl::false_type` otherwise.
51///
52/// This trait is particularly useful for implementing functions that accept
53/// "string-view-like" types, as specified in C++17 and later standards.
54///
55/// ## Usage {#bslstl_isconvertibletostringview-usage}
56///
57///
58/// In this section we show the intended use of this component.
59///
60/// ### Example 1: Determine If a Type Is Convertible to a String View {#bslstl_isconvertibletostringview-example-1-determine-if-a-type-is-convertible-to-a-string-view}
61///
62///
63/// Suppose that we want to determine whether various types are convertible to
64/// `bsl::string_view`.
65///
66/// First, we note that `bsl::string_view` itself is convertible:
67/// @code
68/// assert((bslstl::IsConvertibleToStringView<char,
69/// std::char_traits<char>,
70/// bsl::string_view>::value));
71/// @endcode
72/// Next, we verify that `const char *` is convertible to string_view:
73/// @code
74/// assert((bslstl::IsConvertibleToStringView<char,
75/// std::char_traits<char>,
76/// const char *>::value));
77/// @endcode
78/// Finally, we verify that an unrelated type is not convertible:
79/// @code
80/// assert((!bslstl::IsConvertibleToStringView<char,
81/// std::char_traits<char>,
82/// int>::value));
83/// @endcode
84/// @}
85/** @} */
86/** @} */
87
88/** @addtogroup bsl
89 * @{
90 */
91/** @addtogroup bslstl
92 * @{
93 */
94/** @addtogroup bslstl_isconvertibletostringview
95 * @{
96 */
97
98#include <bslscm_version.h>
99
100#include <bslstl_stringview.h>
101
103#include <bslmf_isconvertible.h>
104
105
106namespace bslstl {
107
108 // ==================================
109 // struct IsConvertibleToStringView
110 // ==================================
111
112/// This `struct` template implements a meta-function to determine if the
113/// (template parameter) `t_TYPE` is convertible to
114/// `bsl::basic_string_view<t_CHAR_TYPE, t_CHAR_TRAITS>`.
115template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_TYPE>
117: bsl::is_convertible<t_TYPE,
118 bsl::basic_string_view<t_CHAR_TYPE, t_CHAR_TRAITS> > {
119};
120
121/// This partial specialization of `IsConvertibleToStringView` is
122/// instantiated when `const t_CHAR_TYPE (&)[]` (an unbounded array
123/// reference) is tested for convertibility to string view. This handles
124/// edge cases with certain compilers where `bsl::is_convertible` may not
125/// correctly handle incomplete array types.
126template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
127struct IsConvertibleToStringView<t_CHAR_TYPE,
128 t_CHAR_TRAITS,
129 const t_CHAR_TYPE (&)[]>
130: bsl::true_type {
131};
132
133} // close package namespace
134
135
136#endif
137
138// ----------------------------------------------------------------------------
139// Copyright 2026 Bloomberg Finance L.P.
140//
141// Licensed under the Apache License, Version 2.0 (the "License");
142// you may not use this file except in compliance with the License.
143// You may obtain a copy of the License at
144//
145// http://www.apache.org/licenses/LICENSE-2.0
146//
147// Unless required by applicable law or agreed to in writing, software
148// distributed under the License is distributed on an "AS IS" BASIS,
149// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
150// See the License for the specific language governing permissions and
151// limitations under the License.
152// ----------------------------- END-OF-FILE ----------------------------------
153
154/** @} */
155/** @} */
156/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
Definition bslstl_algorithm.h:84
Definition bslmf_isconvertible.h:875
Definition bslstl_isconvertibletostringview.h:118