]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | .. Metafunctions/Type Selection//if_ |10 |
2 | ||
3 | if\_ | |
4 | ==== | |
5 | ||
6 | Synopsis | |
7 | -------- | |
8 | ||
9 | .. parsed-literal:: | |
10 | ||
11 | template< | |
12 | typename C | |
13 | , typename T1 | |
14 | , typename T2 | |
15 | > | |
16 | struct if\_ | |
17 | { | |
18 | typedef |unspecified| type; | |
19 | }; | |
20 | ||
21 | ||
22 | ||
23 | Description | |
24 | ----------- | |
25 | ||
26 | Returns one of its two arguments, ``T1`` or ``T2``, depending on the value ``C``. | |
27 | ||
28 | ||
29 | Header | |
30 | ------ | |
31 | ||
32 | .. parsed-literal:: | |
33 | ||
34 | #include <boost/mpl/if.hpp> | |
35 | ||
36 | ||
37 | Parameters | |
38 | ---------- | |
39 | ||
40 | +---------------+-----------------------------------+-----------------------------------------------+ | |
41 | | Parameter | Requirement | Description | | |
42 | +===============+===================================+===============================================+ | |
43 | | ``C`` | |Integral Constant| | A selection condition. | | |
44 | +---------------+-----------------------------------+-----------------------------------------------+ | |
45 | | ``T1``, ``T2``| Any type | Types to select from. | | |
46 | +---------------+-----------------------------------+-----------------------------------------------+ | |
47 | ||
48 | ||
49 | Expression semantics | |
50 | -------------------- | |
51 | ||
52 | For any |Integral Constant| ``c`` and arbitrary types ``t1``, ``t2``: | |
53 | ||
54 | ||
55 | .. parsed-literal:: | |
56 | ||
57 | typedef if_<c,t1,t2>::type t; | |
58 | ||
59 | :Return type: | |
60 | Any type. | |
61 | ||
62 | :Semantics: | |
63 | If ``c::value == true``, ``t`` is identical to ``t1``; otherwise ``t`` is | |
64 | identical to ``t2``. | |
65 | ||
66 | ||
67 | Example | |
68 | ------- | |
69 | ||
70 | .. parsed-literal:: | |
71 | ||
72 | typedef if\_<true\_,char,long>::type t1; | |
73 | typedef if\_<false\_,char,long>::type t2; | |
74 | ||
75 | BOOST_MPL_ASSERT(( is_same<t1, char> )); | |
76 | BOOST_MPL_ASSERT(( is_same<t2, long> )); | |
77 | ||
78 | ||
79 | .. parsed-literal:: | |
80 | ||
81 | // allocates space for an object of class T on heap or "inplace" | |
82 | // depending on its size | |
83 | template< typename T > struct lightweight | |
84 | { | |
85 | // ... | |
86 | typedef typename if\_< | |
87 | less_equal< sizeof\_<T>, sizeof\_<T*> > | |
88 | , inplace_storage<T> | |
89 | , heap_storage<T> | |
90 | >::type impl_t; | |
91 | ||
92 | impl_t impl; | |
93 | }; | |
94 | ||
95 | ||
96 | See also | |
97 | -------- | |
98 | ||
99 | |Metafunctions|, |Integral Constant|, |if_c|, |eval_if| | |
100 | ||
101 | ||
102 |