BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_if.h
Go to the documentation of this file.
1/// @file bslmf_if.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_if.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_IF
9#define INCLUDED_BSLMF_IF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_if bslmf_if
15/// @brief Provide a compile-time `if/else` (conditional) meta-function.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_if
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_if-purpose"> Purpose</a>
25/// * <a href="#bslmf_if-classes"> Classes </a>
26/// * <a href="#bslmf_if-description"> Description </a>
27/// * <a href="#bslmf_if-meta-function-return-types-and-values"> Meta-Function Return Types and Values </a>
28/// * <a href="#bslmf_if-usage"> Usage </a>
29///
30/// # Purpose {#bslmf_if-purpose}
31/// Provide a compile-time `if/else` (conditional) meta-function.
32///
33/// @deprecated Use @ref bslmf_conditional instead.
34///
35/// # Classes {#bslmf_if-classes}
36///
37/// - bslmf::If: meta-function for compile-time selection of one of two types
38///
39/// @see bslmf_assert, bslmf_nil
40///
41/// # Description {#bslmf_if-description}
42/// This component contains the template class meta-function
43/// `bslmf::If` that is parameterized on three arguments. The first argument is
44/// a (compile-time constant) integral expression that is interpreted by the
45/// meta-function as a boolean conditional; the other two parameters accept type
46/// arguments. If the value of the first argument is non-zero (true), the
47/// meta-function "returns" its second argument (i.e., corresponding to the
48/// first type parameter); otherwise it returns its third argument (the second
49/// type parameter). If the selected type argument is not explicitly specified,
50/// the meta-function returns the default `bslmf::Nil` type.
51///
52/// ## Meta-Function Return Types and Values {#bslmf_if-meta-function-return-types-and-values}
53///
54///
55/// A meta-function is a class template that evaluates, at compile-time, to one
56/// or more types and values. An example of a simple meta-function that adds
57/// two (compile-type constant) integer values is the following `Plus` class
58/// template:
59/// @code
60/// template <int A, int B>
61/// struct Plus {
62/// enum { VALUE = A + B }; // 'VALUE' is meta-function result
63/// };
64/// @endcode
65/// The initializer of the `VALUE` enumerator is the compile-time summation of
66/// the constant values `A` and `B`. The result "returned" by `Plus` is
67/// provided by the `VALUE` enumerator.
68///
69/// An example where a type is returned from a meta-function rather than a value
70/// is illustrated by `SelectLarger` below. The `SelectLarger` meta-function
71/// selects the larger of two types. The result "returned" by `SelectLarger` is
72/// provided by the `SelectLarger<...>::Type` `typedef`:
73/// @code
74/// template <class T1, class T2>
75/// struct SelectLarger {
76/// template <class U1, class U2, bool V = (sizeof(T1) > sizeof(T2))>
77/// struct Impl {
78/// typedef T1 Type;
79/// };
80/// template <class U1, class U2>
81/// struct Impl <U1, U2, false> {
82/// typedef T2 Type;
83/// };
84///
85/// typedef typename Impl<T1, T2>::Type Type; // 'Type' is meta-function
86/// // result
87/// };
88/// @endcode
89/// The preceding two examples illustrate the naming conventions used throughout
90/// `bslmf` to denote the types and values returned by meta-functions. In
91/// particular, enumerators or `static const` integral variables named `VALUE`
92/// provide the results of value-returning meta-functions, and nested types
93/// named `Type` provide the results of type-returning meta-functions.
94///
95/// ## Usage {#bslmf_if-usage}
96///
97///
98/// The following snippets of code illustrate basic use of the `bslmf::If`
99/// meta-function. The examples make use of the following declarations to
100/// identify the type that is selected by a given constant integral expression:
101/// @code
102/// enum TypeCode { T_UNKNOWN = 0, T_CHAR = 1, T_INT = 2, T_NIL = 3 };
103///
104/// TypeCode whatType(char) { return T_CHAR; }
105/// TypeCode whatType(int) { return T_INT; }
106/// TypeCode whatType(bslmf::Nil) { return T_NIL; }
107/// TypeCode whatType(...) { return T_UNKNOWN; }
108/// @endcode
109/// In the following example, the meta-function condition (the first argument to
110/// `bslmf::If`) evaluates to true (non-zero). Thus, `bslmf::If<...>::Type` is
111/// a synonym for `int`; i.e., it "evaluates" (at compile time) to `int`:
112/// @code
113/// typedef int T1; assert(1 < sizeof(T1));
114/// typedef char T2; assert(1 == sizeof(T2));
115///
116/// typedef bslmf::If<(sizeof(T1) > sizeof(T2)), T1, T2>::Type LargerType;
117/// assert(T_INT == whatType(LargerType()));
118/// @endcode
119/// In the next example, the condition argument evaluates to false (zero). In
120/// this case, `bslmf::If<...>::Type` evaluates to `bslmf::Nil` since the third
121/// template argument (the "else" type) is not explicitly specified:
122/// @code
123/// typedef bslmf::If<(sizeof(T2) > 1), int>::Type Type2;
124/// assert(T_NIL == whatType(Type2()));
125/// @endcode
126/// @}
127/** @} */
128/** @} */
129
130/** @addtogroup bsl
131 * @{
132 */
133/** @addtogroup bslmf
134 * @{
135 */
136/** @addtogroup bslmf_if
137 * @{
138 */
139
140#include <bslscm_version.h>
141
142#include <bslmf_conditional.h>
143#include <bslmf_nil.h>
144
145
146
147namespace bslmf {
148
149 // =========
150 // struct If
151 // =========
152
153/// This meta-function selects `t_IF_TRUE_TYPE` if `t_CONDITION` is
154/// non-zero. and `t_IF_FALSE_TYPE` otherwise.
155template <int t_CONDITION,
156 class t_IF_TRUE_TYPE = Nil,
157 class t_IF_FALSE_TYPE = Nil>
158struct If {
159
160 typedef typename bsl::
161 conditional<t_CONDITION, t_IF_TRUE_TYPE, t_IF_FALSE_TYPE>::type Type;
162};
163
164} // close package namespace
165
166#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
167// ============================================================================
168// BACKWARD COMPATIBILITY
169// ============================================================================
170
171#ifdef bslmf_If
172#undef bslmf_If
173#endif
174/// This alias is defined for backward compatibility.
175#define bslmf_If bslmf::If
176#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
177
178
179
180#endif
181
182// ----------------------------------------------------------------------------
183// Copyright 2013 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
Definition bslmf_if.h:158
bsl::conditional< t_CONDITION, t_IF_TRUE_TYPE, t_IF_FALSE_TYPE >::type Type
Definition bslmf_if.h:161