-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_time_type_deduction.cpp
43 lines (34 loc) · 1.1 KB
/
compile_time_type_deduction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
#include <type_traits>
using namespace std;
class exact{};
class floating{};
template <class T>
constexpr bool close_enough(T a, T b, exact) {
return a == b;
}
template <class T>
constexpr bool close_enough(T a, T b, floating) {
return abs(a - b) <= static_cast<T>(0.000001);
}
//since abs is not constexpr then anything that calls it is not compile time
//to get around this we would need to write a constexpr equivilant
template <class T>
constexpr bool close_enough(T a, T b) {
//old way to write this, shit it is so ugly or whatever
return close_enough(a, b, typename conditional<is_floating_point<T>::value, floating, exact>::type{});
//new way, replace typename ::type with _t and ::value with _v added in C++17
//return close_enough(a, b, conditional_t<is_floating_point_v<T>, floating, exact>{});
}
int main() {
if(close_enough(3,4)) {
cout << "Booooo" << endl;
}
if(close_enough(3.1, 3.000000002)) {
cout << "what" << endl;
}
if(close_enough(3.1, 3.1000000009)) {
cout << "this is good" << endl;
}
}