BDE 4.14.0 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// Include version that can be compiled with C++03
110// Generated on Fri Dec 16 11:47:20 2022
111// Command line: sim_cpp11_features.pl bslmf_nthparameter.h
112# define COMPILING_BSLMF_NTHPARAMETER_H
114# undef COMPILING_BSLMF_NTHPARAMETER_H
115#else
116
117
118
119namespace bslmf {
120
121 // ===========================
122 // class template NthParameter
123 // ===========================
124
125struct NthParameter_Sentinel; // Declared but not defined
126
127#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=15
128
129/// Metafunction to compute the specified `t_N`th element of the specified
130/// `t_PARAMS` template parameter pack. The `Type` nested typedef will
131/// match the `t_N`th element of `t_PARAMS`, where `t_N` is zero-based (so
132/// that an `t_N` of zero corresponds to the first parameter.
133template <std::size_t t_N,
134 class t_FIRST_PARAM = NthParameter_Sentinel,
135 class... t_PARAMS>
137
138 /// The type of the Nth parameter, computed by recursively stripping off
139 /// the first parameter until t_N == 0.
140 typedef typename NthParameter<t_N - 1, t_PARAMS...>::Type Type;
141};
142
143// ============================================================================
144// IMPLEMENTATION
145// ============================================================================
146
147/// Specialization of `NthParameter` for when `t_N` is zero.
148template <class t_FIRST_PARAM, class... t_PARAMS>
149struct NthParameter<0, t_FIRST_PARAM, t_PARAMS...> {
150
151 /// The type of the 0th parameter.
152 typedef t_FIRST_PARAM Type;
153};
154
155#endif
156
157/// Specialization of `NthParameter` for when `t_N` exceeds the actual
158/// number of parameters.
159template <>
160struct NthParameter<0, NthParameter_Sentinel> {
161
162#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES
163 // No 'Type' member is defined.
164#else
165 // There are no dependent parameters because this is a full specialization.
166 // When used in another simulated variadic template, the compiler may
167 // attempt to evaluate the 'Type' member even when that client is not
168 // actually instantiated. To avoid a spurious compilation error, we must
169 // therefore make sure that 'Type' is defined, even if it is defined as an
170 // incomplete class.
171 typedef NthParameter_Sentinel Type;
172#endif
173};
174
175} // close package namespace
176
177
178#endif // End C++11 code
179
180#endif // ! defined(INCLUDED_BSLMF_NTHPARAMETER)
181
182// ----------------------------------------------------------------------------
183// Copyright 2019 Bloomberg Finance L.P.
184//
185// Licensed under the Apache License, Version 2.0 (the "License");
186// you may not use this file except in compliance with the License.
187// You may obtain a copy of the License at
188//
189// http://www.apache.org/licenses/LICENSE-2.0
190//
191// Unless required by applicable law or agreed to in writing, software
192// distributed under the License is distributed on an "AS IS" BASIS,
193// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
194// See the License for the specific language governing permissions and
195// limitations under the License.
196// ----------------------------- END-OF-FILE ----------------------------------
197
198/** @} */
199/** @} */
200/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlbb_blob.h:576
NthParameter_Sentinel Type
Definition bslmf_nthparameter.h:171
t_FIRST_PARAM Type
The type of the 0th parameter.
Definition bslmf_nthparameter.h:152
Definition bslmf_nthparameter.h:136
NthParameter< t_N-1, t_PARAMS... >::Type Type
Definition bslmf_nthparameter.h:140