Last active
June 11, 2020 21:13
-
-
Save peterhillman/b233c344918be066ad6704e378a71024 to your computer and use it in GitHub Desktop.
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 <iostream> | |
// traits to define which version is used | |
struct throwexceptions{}; | |
struct dontthrowexceptions{}; | |
const throwexceptions Throw; | |
template<typename T> class Bob | |
{ | |
public: | |
// | |
// throwing version with 'opt-in' parameter | |
// | |
void invert(const throwexceptions& state = Throw) | |
{ | |
std::cerr << "throwing\n"; | |
throw 1; | |
} | |
// | |
// non-throwing version with 'opt-out' parameter | |
// | |
inline void invert(const dontthrowexceptions&) noexcept | |
{ | |
std::cerr << "not throwing\n"; | |
} | |
T lighter; | |
T darker; | |
}; | |
int main(void) | |
{ | |
Bob<int> b; | |
try | |
{ | |
b.invert(); // throw exception | |
} catch(...) { } | |
b.invert( dontthrowexceptions() ); | |
// | |
// decide at compile-time whether to throw exceptions | |
// | |
typedef throwexceptions shouldThrow; | |
b.invert( shouldThrow() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment