Skip to content

Instantly share code, notes, and snippets.

@bryce-ebp
Last active July 18, 2019 07:28
Show Gist options
  • Save bryce-ebp/048b3a3c67ee137ae999db407745eb6b to your computer and use it in GitHub Desktop.
Save bryce-ebp/048b3a3c67ee137ae999db407745eb6b to your computer and use it in GitHub Desktop.
Simple option handler for settings class
#include <variant>
template<typename T>
class Option {
public:
Option(const T val)
: variant(val) { }
const T &Get() const {
return std::get<T>(variant);
}
operator bool() const {
return std::get<T>(variant);
}
private:
std::variant<int, bool, float> variant{ };
};
// example use:
class Settings {
public:
Option<bool> value_{ false };
Option<bool> other_value_{ true };
};
int main( ) {
Settings setting{ };
if (setting.value_.Get()
|| setting.other_value_.Get())
return 1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment