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