C++ Library Extensions 2022.12.09
To help learn modern C++ programming
005-throw_debug_exception.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3template<typename Type, typename...Types>
4auto divide_sequence(Type&& first, Types&&... args)
5{
6 auto sequence = std::vector<tpf::remove_cv_ref_t<Type>>
7 { std::forward<Type>(first), std::forward<Types>(args)... };
8
9 size_t size = sequence.size();
10 auto result = sequence.front();
11
12 for(size_t i = 1; i < size; ++i)
13 {
14 if( sequence[i] == 0)
15 {
17 stream << "Division By Zero: " << i <<"-th element is zero";
19 }
20
21 result /= sequence[i];
22 }
23
24 return result;
25}
26
27int main()
28{
29 try
30 {
31 // ((2*3*5)/2)/3
32 auto rlt = divide_sequence(2*3*5, 2, 3);
34 stream << "((2*3*5)/2)/3 = " << rlt << tpf::endl;
35
36 stream << "((2*3*5)/0)/3 = " << divide_sequence(2*3*5, 0, 3)
37 << tpf::endl;
38 }
39 catch(std::exception& e)
40 {
41 tpf::console cout;
42 cout << e.what() << tpf::endl;
43 }
44
45}
auto divide_sequence(Type &&first, Types &&... args)
auto & cout
tpf::sstream stream
std::integer_sequence< std::common_type_t< std::remove_cvref_t< decltype(Indices)>... >, Indices... > sequence
Definition: cpg_types.hpp:2665
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.
Definition: tpf_types.hpp:1416