piranha  0.10
invert.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_INVERT_HPP
30 #define PIRANHA_INVERT_HPP
31 
32 #include <type_traits>
33 
34 #include <piranha/detail/sfinae_types.hpp>
35 #include <piranha/pow.hpp>
36 
37 namespace piranha
38 {
39 
40 namespace math
41 {
42 
44 
49 template <typename T, typename = void>
50 struct invert_impl {
52 
59  template <typename U>
60  auto operator()(const U &x) const -> decltype(math::pow(x, -1))
61  {
62  return math::pow(x, -1);
63  }
64 };
65 
67 
78 template <typename T>
79 inline auto invert(const T &x) -> decltype(invert_impl<T>()(x))
80 {
81  return invert_impl<T>()(x);
82 }
83 }
84 
86 
91 template <typename T>
92 class is_invertible : detail::sfinae_types
93 {
94  template <typename T1>
95  static auto test(const T1 &x) -> decltype(math::invert(x), void(), yes());
96  static no test(...);
97 
98 public:
100  static const bool value = std::is_same<decltype(test(std::declval<T>())), yes>::value;
101 };
102 
103 // Static init.
104 template <typename T>
105 const bool is_invertible<T>::value;
106 }
107 
108 #endif
math_pow_t< T, U > pow(const T &x, const U &y)
Exponentiation.
Definition: pow.hpp:126
auto invert(const T &x) -> decltype(invert_impl< T >()(x))
Compute the inverse.
Definition: invert.hpp:79
auto operator()(const U &x) const -> decltype(math::pow(x, -1))
Call operator.
Definition: invert.hpp:60
Type trait for invertible types.
Definition: invert.hpp:92
Root piranha namespace.
Definition: array_key.hpp:52
static const bool value
Value of the type trait.
Definition: invert.hpp:100
Default functor for the implementation of piranha::math::invert().
Definition: invert.hpp:50