xref: /petsc/include/petsccxxcomplexfix.h (revision 2fa40bb9206b96114faa7cb222621ec184d31cd2)
1 #if !defined(PETSCCXXCOMPLEXFIX_H)
2 #define PETSCCXXCOMPLEXFIX_H
3 
4 /*
5     The pragma below silence all compiler warnings comming from code in this header file.
6     In particular, it silences `-Wfloat-equal` warnings in `operator==()` and `operator!=` below.
7     Other compilers beyond GCC support this pragma.
8 */
9 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__NEC__)
10 #pragma GCC system_header
11 #endif
12 
13 /*
14      Defines additional operator overloading for the C++ complex class that are "missing" in the standard
15      include files. For example, the code fragment
16 
17      std::complex<double> c = 22.0;
18      c = 11 + c;
19 
20      will produce a compile time error such as
21 
22      error: no match for 'operator+' (operand types are 'int' and 'std::complex<double>')
23 
24      The code fragment
25 
26      std::complex<float> c = 22.0;
27      c = 11.0 + c;
28 
29      will produce a compile time error such as
30 
31      error: no match for 'operator+' (operand types are 'double' and 'std::complex<float>')
32 
33      This deficiency means one may need to write cumbersome code while working with the C++ complex classes.
34 
35      This include file defines a few additional operator overload methods for the C++ complex classes to handle
36      these cases naturally within PETSc code.
37 
38      This file is included in petscsystypes.h when feasible. In the small number of cases where these additional methods
39      may conflict with other code one may add '#define PETSC_SKIP_CXX_COMPLEX_FIX 1' before including any PETSc include
40      files to prevent these methods from being provided.
41 */
42 
43 #define PETSC_CXX_COMPLEX_FIX(Type) \
44 static inline PetscComplex operator+(const PetscComplex& lhs, const Type& rhs) { return const_cast<PetscComplex&>(lhs) + PetscReal(rhs); } \
45 static inline PetscComplex operator+(const Type& lhs, const PetscComplex& rhs) { return PetscReal(lhs) + const_cast<PetscComplex&>(rhs); } \
46 static inline PetscComplex operator-(const PetscComplex& lhs, const Type& rhs) { return const_cast<PetscComplex&>(lhs) - PetscReal(rhs); } \
47 static inline PetscComplex operator-(const Type& lhs, const PetscComplex& rhs) { return PetscReal(lhs) - const_cast<PetscComplex&>(rhs); } \
48 static inline PetscComplex operator*(const PetscComplex& lhs, const Type& rhs) { return const_cast<PetscComplex&>(lhs) * PetscReal(rhs); } \
49 static inline PetscComplex operator*(const Type& lhs, const PetscComplex& rhs) { return PetscReal(lhs) * const_cast<PetscComplex&>(rhs); } \
50 static inline PetscComplex operator/(const PetscComplex& lhs, const Type& rhs) { return const_cast<PetscComplex&>(lhs) / PetscReal(rhs); } \
51 static inline PetscComplex operator/(const Type& lhs, const PetscComplex& rhs) { return PetscReal(lhs) / const_cast<PetscComplex&>(rhs); } \
52 static inline bool operator==(const PetscComplex& lhs, const Type& rhs) { return const_cast<PetscComplex&>(lhs).imag() == PetscReal(0) && const_cast<PetscComplex&>(lhs).real() == PetscReal(rhs); } \
53 static inline bool operator==(const Type& lhs, const PetscComplex& rhs) { return const_cast<PetscComplex&>(rhs).imag() == PetscReal(0) && const_cast<PetscComplex&>(rhs).real() == PetscReal(lhs); } \
54 static inline bool operator!=(const PetscComplex& lhs, const Type& rhs) { return const_cast<PetscComplex&>(lhs).imag() != PetscReal(0) || const_cast<PetscComplex&>(lhs).real() != PetscReal(rhs); } \
55 static inline bool operator!=(const Type& lhs, const PetscComplex& rhs) { return const_cast<PetscComplex&>(rhs).imag() != PetscReal(0) || const_cast<PetscComplex&>(rhs).real() != PetscReal(lhs); } \
56 /* PETSC_CXX_COMPLEX_FIX */
57 
58 /*
59     Due to the C++ automatic promotion rules for floating point and integer values only the two cases below
60     need to be handled.
61 */
62 #if defined(PETSC_USE_REAL_SINGLE)
63 PETSC_CXX_COMPLEX_FIX(double)
64 #elif defined(PETSC_USE_REAL_DOUBLE)
65 PETSC_CXX_COMPLEX_FIX(PetscInt)
66 #endif /* PETSC_USE_REAL_* */
67 
68 #endif
69