Skip to content

Instantly share code, notes, and snippets.

@peterhillman
Last active June 11, 2020 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhillman/b233c344918be066ad6704e378a71024 to your computer and use it in GitHub Desktop.
Save peterhillman/b233c344918be066ad6704e378a71024 to your computer and use it in GitHub Desktop.
#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