Last active
June 20, 2024 11:12
-
-
Save mmomtchev/32753950ca58c6de9a294891d40b150a to your computer and use it in GitHub Desktop.
Gists for the C++ exceptions medium story
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdexcept> | |
#include <stdio.h> | |
static int objects; | |
class IntWithDestructor { | |
int val; | |
public: | |
IntWithDestructor() : val{} { objects++; } | |
~IntWithDestructor() { objects--; } | |
int operator=(int rhs) { | |
val = rhs; | |
return val; | |
} | |
operator int() { return val; } | |
}; | |
static int result; | |
int fibonacci(int i) | |
#ifdef unwind_noexcept | |
noexcept | |
#endif | |
{ | |
// Make sure there is stack unwinding code | |
IntWithDestructor a, b; | |
// Optimize the branch prediction | |
if (i <= 1) [[unlikely]] { | |
if (i < 0) | |
#if defined(unwind_throwing) || defined(unwind_catching) | |
throw std::runtime_error{"Negative number"}; | |
#else | |
std::abort(); | |
#endif | |
if (i == 0) | |
return 0; | |
return 1; | |
} | |
#ifdef unwind_catching | |
try { | |
#endif | |
a = fibonacci(i - 1); | |
b = fibonacci(i - 2); | |
return (int)a + (int)b; | |
#ifdef unwind_catching | |
} catch (const std::exception &) { | |
std::abort(); | |
} | |
#endif | |
} | |
int main() { | |
#if defined(unwind_throwing) || defined(unwind_catching) | |
try { | |
#endif | |
for (int i = 0; i < 46; i++) { | |
result = fibonacci(i); | |
} | |
#if defined(unwind_throwing) || defined(unwind_catching) | |
} catch (const std::exception &) { | |
std::abort(); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment