QMCPACK
TypeComputations.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // ACL:license
3 // ----------------------------------------------------------------------
4 // This software and ancillary information (herein called "SOFTWARE")
5 // called PETE (Portable Expression Template Engine) is
6 // made available under the terms described here. The SOFTWARE has been
7 // approved for release with associated LA-CC Number LA-CC-99-5.
8 //
9 // Unless otherwise indicated, this SOFTWARE has been authored by an
10 // employee or employees of the University of California, operator of the
11 // Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with
12 // the U.S. Department of Energy. The U.S. Government has rights to use,
13 // reproduce, and distribute this SOFTWARE. The public may copy, distribute,
14 // prepare derivative works and publicly display this SOFTWARE without
15 // charge, provided that this Notice and any statement of authorship are
16 // reproduced on all copies. Neither the Government nor the University
17 // makes any warranty, express or implied, or assumes any liability or
18 // responsibility for the use of this SOFTWARE.
19 //
20 // If SOFTWARE is modified to produce derivative works, such modified
21 // SOFTWARE should be clearly marked, so as not to confuse it with the
22 // version available from LANL.
23 //
24 // For more information about PETE, send e-mail to pete@acl.lanl.gov,
25 // or visit the PETE web page at http://www.acl.lanl.gov/pete/.
26 // ----------------------------------------------------------------------
27 // ACL:license
28 
29 #ifndef PETE_PETE_TYPECOMPUTATIONS_H
30 #define PETE_PETE_TYPECOMPUTATIONS_H
31 
32 namespace qmcplusplus
33 {
34 //-----------------------------------------------------------------------------
35 //
36 // CLASS NAME
37 // UnaryReturn<T, Op>
38 //
39 // DESCRIPTION
40 // This template describes the default mechanism for calculating the
41 // return type to a unary expression given the argument type T and
42 // the operation type Op.
43 //
44 // There are a several sensible things one can do:
45 // o make the return type the same as the argument to the
46 // function/operation. For example, operator-(T) should return a T.
47 // o return a type based entirely on the operation.
48 // For example, operator! always returns a bool.
49 // o sythesize a type based on the type of the argument and the operation.
50 // The first case is most common. We therefore make it the behavior
51 // for the base template. The other cases are handled by partial
52 // specialization.
53 //
54 // For example, the abs function typically returns a double when the
55 // argument is a std::complex<double>. The appropriate specialization here
56 // would be:
57 // template<> struct PETEUnaryReturn<std::complex<double>, FnAbs> {
58 // using Type_t = double;
59 // };
60 //
61 //-----------------------------------------------------------------------------
62 
63 template<class T, class Op>
65 {
66  using Type_t = T;
67 };
68 
69 //-----------------------------------------------------------------------------
70 //
71 // CLASS NAME
72 // Promote<T1, T2>
73 //
74 // DESCRIPTION
75 // General template and specializations to implement C++ type promotion
76 // for basic types.
77 //
78 //-----------------------------------------------------------------------------
79 
80 // Base template: don't do anything by default.
81 
82 template<class T1, class T2>
83 struct Promote
84 {
85  using Type_t = T1;
86 };
87 
88 // bool
89 
90 template<>
91 struct Promote<bool, bool>
92 {
93  using Type_t = bool;
94 };
95 
96 template<>
97 struct Promote<bool, char>
98 {
99  using Type_t = char;
100 };
101 
102 template<>
103 struct Promote<bool, short>
104 {
105  using Type_t = short;
106 };
107 
108 template<>
109 struct Promote<bool, int>
110 {
111  using Type_t = int;
112 };
113 
114 template<>
115 struct Promote<bool, long>
116 {
117  using Type_t = long;
118 };
119 
120 template<>
121 struct Promote<bool, float>
122 {
123  using Type_t = float;
124 };
125 
126 template<>
127 struct Promote<bool, double>
128 {
129  using Type_t = double;
130 };
131 
132 // char
133 
134 template<>
135 struct Promote<char, bool>
136 {
137  using Type_t = char;
138 };
139 
140 template<>
141 struct Promote<char, char>
142 {
143  using Type_t = char;
144 };
145 
146 template<>
147 struct Promote<char, short>
148 {
149  using Type_t = short;
150 };
151 
152 template<>
153 struct Promote<char, int>
154 {
155  using Type_t = int;
156 };
157 
158 template<>
159 struct Promote<char, long>
160 {
161  using Type_t = long;
162 };
163 
164 template<>
165 struct Promote<char, float>
166 {
167  using Type_t = float;
168 };
169 
170 template<>
171 struct Promote<char, double>
172 {
173  using Type_t = double;
174 };
175 
176 // short
177 
178 template<>
179 struct Promote<short, bool>
180 {
181  using Type_t = short;
182 };
183 
184 template<>
185 struct Promote<short, char>
186 {
187  using Type_t = short;
188 };
189 
190 template<>
191 struct Promote<short, short>
192 {
193  using Type_t = short;
194 };
195 
196 template<>
197 struct Promote<short, int>
198 {
199  using Type_t = int;
200 };
201 
202 template<>
203 struct Promote<short, long>
204 {
205  using Type_t = long;
206 };
207 
208 template<>
209 struct Promote<short, float>
210 {
211  using Type_t = float;
212 };
213 
214 template<>
215 struct Promote<short, double>
216 {
217  using Type_t = double;
218 };
219 
220 // int
221 
222 template<>
223 struct Promote<int, bool>
224 {
225  using Type_t = int;
226 };
227 
228 template<>
229 struct Promote<int, char>
230 {
231  using Type_t = int;
232 };
233 
234 template<>
235 struct Promote<int, short>
236 {
237  using Type_t = int;
238 };
239 
240 template<>
241 struct Promote<int, int>
242 {
243  using Type_t = int;
244 };
245 
246 template<>
247 struct Promote<int, long>
248 {
249  using Type_t = long;
250 };
251 
252 template<>
253 struct Promote<int, float>
254 {
255  using Type_t = float;
256 };
257 
258 template<>
259 struct Promote<int, double>
260 {
261  using Type_t = double;
262 };
263 
264 // long
265 
266 template<>
267 struct Promote<long, bool>
268 {
269  using Type_t = long;
270 };
271 
272 template<>
273 struct Promote<long, char>
274 {
275  using Type_t = long;
276 };
277 
278 template<>
279 struct Promote<long, short>
280 {
281  using Type_t = long;
282 };
283 
284 template<>
285 struct Promote<long, int>
286 {
287  using Type_t = long;
288 };
289 
290 template<>
291 struct Promote<long, long>
292 {
293  using Type_t = long;
294 };
295 
296 template<>
297 struct Promote<long, float>
298 {
299  using Type_t = float;
300 };
301 
302 template<>
303 struct Promote<long, double>
304 {
305  using Type_t = double;
306 };
307 
308 // float
309 
310 template<>
311 struct Promote<float, bool>
312 {
313  using Type_t = float;
314 };
315 
316 template<>
317 struct Promote<float, char>
318 {
319  using Type_t = float;
320 };
321 
322 template<>
323 struct Promote<float, short>
324 {
325  using Type_t = float;
326 };
327 
328 template<>
329 struct Promote<float, int>
330 {
331  using Type_t = float;
332 };
333 
334 template<>
335 struct Promote<float, long>
336 {
337  using Type_t = float;
338 };
339 
340 template<>
341 struct Promote<float, float>
342 {
343  using Type_t = float;
344 };
345 
346 template<>
347 struct Promote<float, double>
348 {
349  using Type_t = double;
350 };
351 
352 // double
353 
354 template<>
355 struct Promote<double, bool>
356 {
357  using Type_t = double;
358 };
359 
360 template<>
361 struct Promote<double, char>
362 {
363  using Type_t = double;
364 };
365 
366 template<>
367 struct Promote<double, short>
368 {
369  using Type_t = double;
370 };
371 
372 template<>
373 struct Promote<double, int>
374 {
375  using Type_t = double;
376 };
377 
378 template<>
379 struct Promote<double, long>
380 {
381  using Type_t = double;
382 };
383 
384 template<>
385 struct Promote<double, float>
386 {
387  using Type_t = double;
388 };
389 
390 template<>
391 struct Promote<double, double>
392 {
393  using Type_t = double;
394 };
395 
396 //-----------------------------------------------------------------------------
397 //
398 // CLASS NAME
399 // BinaryReturn<T1, T2, Op>
400 //
401 // DESCRIPTION
402 // This template describes the default mechanism for calculating the
403 // return type to a binary expression given the argument types T1 and
404 // T2 and the operation type Op.
405 //
406 // There are several sensible things one can do:
407 // o make the return type by promoting/converting the "simpler" type
408 // into the more "complex." For example, we typically want to do
409 // this with addition.
410 // o return the type of the left-hand operand. For example, this is
411 // what happens with operator<<.
412 // o return the type of the right-hand operand.
413 // o return a type based entirely on the operation.
414 // For example, operator!= always returns a bool.
415 // o synthesize the return type based on the operation and types.
416 // The first option is most common, so we make that the behavior of the
417 // base template. The other cases are handled by partial specialization.
418 //
419 // For example, the multiplication between a matrix and a vector might do a
420 // matrix/vector product, thereby returning a vector. The appropriate
421 // specialization here would be:
422 // struct BinaryReturn<Mat<double,3>, Vec<float,3>, OpMultiply> {
423 // using Type_t = Vec<double,3>;
424 // };
425 // Notice how the element type is promoted.
426 //
427 //-----------------------------------------------------------------------------
428 
429 template<class T1, class T2, class Op>
431 {
432  using Type_t = typename Promote<T1, T2>::Type_t;
433 };
434 
435 
436 //-----------------------------------------------------------------------------
437 //
438 // CLASS NAME
439 // TrinaryReturn<T1, T2, T3, Op>
440 //
441 // DESCRIPTION
442 // This template describes the default mechanism for calculating the
443 // return type to a trinary expression given the argument types T1, T2, and
444 // T3 and the operation type Op. The only trinary expression supported
445 // in C++ is the ?: operation. In this case, T1 should end up being bool
446 // and the result of the calculation is of type Binary_Promotion(T2,T3)
447 // with the value being that associated with T2 if T1's associated value
448 // turns out to be true and T3 if T1's associated value turns out to be
449 // false.
450 //
451 //-----------------------------------------------------------------------------
452 
453 template<class T1, class T2, class T3, class Op>
455 {
457 };
458 
459 } // namespace qmcplusplus
460 
461 #endif // PETE_PETE_TYPE_COMPUTATIONS_H
462 
463 // ACL:rcsinfo
typename Promote< T1, T2 >::Type_t Type_t
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
typename BinaryReturn< T2, T3, Op >::Type_t Type_t