-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacktrace-fib.cpp
50 lines (39 loc) · 1007 Bytes
/
stacktrace-fib.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
44
45
46
47
48
49
50
#include <iostream>
#include <format>
#include <stacktrace>
class fib_with_stacktrace {
std::stacktrace::size_type max_depth = 0;
std::stacktrace max_trace;
public:
fib_with_stacktrace() = default;
// Return the nth number in the fibonaci sequence
unsigned int fib(unsigned int n)
{
auto trace = std::stacktrace::current();
auto depth = trace.size();
if (trace.size() > max_depth) {
max_trace = trace;
max_depth = depth;
}
return n < 2 ? n : fib(n-1) + fib(n-2);
}
std::stacktrace get_max_trace() const
{
return max_trace;
}
};
int main(void)
{
fib_with_stacktrace fib_wst;
for (int i=0; i<10; ++i)
std::cout << fib_wst.fib(i) << ' ';
std::cout << '\n';
const std::stacktrace& trace = fib_wst.get_max_trace();
std::cout << "Max stack depth was: " << trace.size() << '\n';
for (const auto& t : trace)
std::cout <<
std::format (
"{}:{} {}\n",
t.source_file(), t.source_line(), t.description()
);
}