BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_nthparameter.h
Go to the documentation of this file.
1/// @file bslmf_nthparameter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_nthparameter.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_NTHPARAMETER
9#define INCLUDED_BSLMF_NTHPARAMETER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_nthparameter bslmf_nthparameter
15/// @brief Metafunction to return the Nth type parameter in a parameter pack
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_nthparameter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_nthparameter-purpose"> Purpose</a>
25/// * <a href="#bslmf_nthparameter-classes"> Classes </a>
26/// * <a href="#bslmf_nthparameter-description"> Description </a>
27/// * <a href="#bslmf_nthparameter-usage"> Usage </a>
28///
29/// # Purpose {#bslmf_nthparameter-purpose}
30/// Metafunction to return the Nth type parameter in a parameter pack
31///
32/// # Classes {#bslmf_nthparameter-classes}
33/// bslmf::NthParameter<t_N, PARAM_0, t_PARAMS...>
34///
35/// @see
36///
37/// # Description {#bslmf_nthparameter-description}
38/// This component contains a metafunction that treats a
39/// parameter pack of types as compile-time array of types, returning the Nth
40/// type (counting from zero). It is useful for implementing types like
41/// `tuple` that need access to a specific element of a parameter pack.
42///
43/// ## Usage {#bslmf_nthparameter-usage}
44///
45///
46/// We wish to implement a `tuple`-like class that holds a heterogeneous
47/// collection of elements, each of which might have a different type. The
48/// metafunction, `my_tuple_element<I, my_tuple<ELEMS...>>::Type` would be type
49/// of the `I`th element in the tuple (where `I` is zero-based).
50///
51/// First, we define our `my_tuple` class template. The body of the class is
52/// unimportant for this usage examples:
53/// @code
54/// template <class... ELEMS>
55/// class my_tuple {
56/// // ...
57/// };
58/// @endcode
59/// Then, we use `bslmf::NthParameter` to implement `my_tuple_element`:
60/// @code
61/// #include <bslmf_nthparameter.h>
62///
63/// template <std::size_t I, class TUPLE>
64/// struct my_tuple_element; // Not defined
65///
66/// template <std::size_t I, class... ELEMS>
67/// struct my_tuple_element<I, my_tuple<ELEMS...> > {
68/// typedef typename bslmf::NthParameter<I, ELEMS...>::Type Type;
69/// };
70/// @endcode
71/// Finally, we test this implementation using `bsl::is_same`:
72/// @code
73/// #include <bslmf_issame.h>
74///
75/// int main()
76/// {
77/// typedef my_tuple<int, short, char*> ttype;
78///
79/// assert((bsl::is_same<int, my_tuple_element<0, ttype>::Type>::value));
80/// assert((bsl::is_same<short, my_tuple_element<1, ttype>::Type>::value));
81/// assert((bsl::is_same<char *, my_tuple_element<2, ttype>::Type>::value));
82///
83/// assert(! (bsl::is_same<short, my_tuple_element<0, ttype>::Type>::value));
84/// }
85/// @endcode
86/// @}
87/** @} */
88/** @} */
89
90/** @addtogroup bsl
91 * @{
92 */
93/** @addtogroup bslmf
94 * @{
95 */
96/** @addtogroup bslmf_nthparameter
97 * @{
98 */
99
100#include <bsls_compilerfeatures.h>
101
102#include <bslmf_assert.h>
103
105
106#include <cstddef>
107
108#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
109// clang-format off
110// Include version that can be compiled with C++03
111// Generated on Mon Jan 13 08:31:25 2025
112// Command line: sim_cpp11_features.pl bslmf_nthparameter.h
113
114# define COMPILING_BSLMF_NTHPARAMETER_H
116# undef COMPILING_BSLMF_NTHPARAMETER_H
117
118// clang-format on
119#else
120
121
122
123namespace bslmf {
124
125 // ===========================
126 // class template NthParameter
127 // ===========================
128
129struct NthParameter_Sentinel; // Declared but not defined
130
131#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=15
132
133/// Metafunction to compute the specified `t_N`th element of the specified
134/// `t_PARAMS` template parameter pack. The `Type` nested typedef will
135/// match the `t_N`th element of `t_PARAMS`, where `t_N` is zero-based (so
136/// that an `t_N` of zero corresponds to the first parameter.
137///
138/// See @ref bslmf_nthparameter
139template <std::size_t t_N,
140 class t_FIRST_PARAM = NthParameter_Sentinel,
141 class... t_PARAMS>
143
144 /// The type of the Nth parameter, computed by recursively stripping off
145 /// the first parameter until t_N == 0.
146 typedef typename NthParameter<t_N - 1, t_PARAMS...>::Type Type;
147};
148
149// ============================================================================
150// IMPLEMENTATION
151// ============================================================================
152
153/// Specialization of `NthParameter` for when `t_N` is zero.
154template <class t_FIRST_PARAM, class... t_PARAMS>
155struct NthParameter<0, t_FIRST_PARAM, t_PARAMS...> {
156
157 /// The type of the 0th parameter.
158 typedef t_FIRST_PARAM Type;
159};
160
161#endif
162
163/// Specialization of `NthParameter` for when `t_N` exceeds the actual
164/// number of parameters.
165template <>
166struct NthParameter<0, NthParameter_Sentinel> {
167
168#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES
169 // No 'Type' member is defined.
170#else
171 // There are no dependent parameters because this is a full specialization.
172 // When used in another simulated variadic template, the compiler may
173 // attempt to evaluate the 'Type' member even when that client is not
174 // actually instantiated. To avoid a spurious compilation error, we must
175 // therefore make sure that 'Type' is defined, even if it is defined as an
176 // incomplete class.
177 typedef NthParameter_Sentinel Type;
178#endif
179};
180
181} // close package namespace
182
183
184#endif // End C++11 code
185
186#endif // ! defined(INCLUDED_BSLMF_NTHPARAMETER)
187
188// ----------------------------------------------------------------------------
189// Copyright 2019 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
NthParameter_Sentinel Type
Definition bslmf_nthparameter.h:177
t_FIRST_PARAM Type
The type of the 0th parameter.
Definition bslmf_nthparameter.h:158
Definition bslmf_nthparameter.h:142
NthParameter< t_N-1, t_PARAMS... >::Type Type
Definition bslmf_nthparameter.h:146