File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * C++17 Structured Biding Example.
3
+ *
4
+ * Compile with something like this:
5
+ *
6
+ * $ clang++-7 -std=c++17 -Wall -pedantic -Werror main.cpp
7
+ */
8
+ #include < iostream>
9
+ #include < tuple>
10
+ #include < cmath>
11
+ #include < limits>
12
+
13
+ static constexpr float EPSILON = 0 .00001f ;
14
+ static constexpr float MONEY_EPSILON = 0 .01f ;
15
+
16
+ using DivideResult = std::tuple<float , const char *>;
17
+
18
+ static DivideResult divide (float x, float y, float eps = EPSILON) {
19
+ if (fabs (y) < eps) {
20
+ return DivideResult{0 .0f , " cannot divide by zero!" };
21
+ }
22
+ return DivideResult{x / y, nullptr };
23
+ }
24
+
25
+ int main () {
26
+ const auto [x, err] = divide (1 .0f , 0 .009f , MONEY_EPSILON);
27
+ if (err) {
28
+ std::cerr << " division error: " << err << ' \n ' ;
29
+ exit (EXIT_FAILURE);
30
+ }
31
+
32
+ std::cout << x << ' \n ' ;
33
+ }
34
+
You can’t perform that action at this time.
0 commit comments