Skip to content

Instantly share code, notes, and snippets.

@blippy
Created August 16, 2017 12:36
Show Gist options
  • Select an option

  • Save blippy/11159ef4d9b96cc350a156410bcb4fed to your computer and use it in GitHub Desktop.

Select an option

Save blippy/11159ef4d9b96cc350a156410bcb4fed to your computer and use it in GitHub Desktop.
Variant in C++
#include <iostream>
#include <string>
#include <stdexcept>
class cc {
public:
cc() { std::cout << "hi" << std::endl; }
~cc() { std::cout << "bye" << std::endl; }
};
enum atype { S };
class atom {
public:
atom(atype a): type(a) {
ptr = new cc;
}
cc* get_str() {
return reinterpret_cast<cc*>(ptr); }
~atom() {
delete reinterpret_cast<cc*>(ptr); }
private:
atype type;
void* ptr = nullptr;
};
int main()
{
try {
atom a(S);
throw std::runtime_error("Opps");
} catch(...) {
std::cout << "caught exception\n" ;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment