Created
April 7, 2017 03:40
-
-
Save dtoma/652dab109dd015edb595033147215299 to your computer and use it in GitHub Desktop.
tuples types
This file contains hidden or 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 <tuple> | |
| // Macro to declare elements of an `enum class` | |
| #define DEFINE_ENUM_TYPE(type, id) type = id, | |
| // Declare currencies | |
| #define CURRENCIES(code) \ | |
| code(USD, 0) \ | |
| code(EUR, 1) | |
| #define CURRENCY_TO_STRING(type, id) case currency::type: return #type; | |
| enum class currency { | |
| CURRENCIES(DEFINE_ENUM_TYPE) | |
| }; | |
| auto currency_to_string(currency c) { | |
| switch (c) { | |
| CURRENCIES(CURRENCY_TO_STRING) | |
| } | |
| return ""; | |
| } | |
| // Declare message types | |
| #define MSG_TYPES(code) \ | |
| code(heartbeat, 0) \ | |
| code(reject, 4) | |
| #define MSG_TYPE_TO_STRING(type, id) case msg_type::type: return #type; | |
| enum class msg_type { | |
| MSG_TYPES(DEFINE_ENUM_TYPE) | |
| }; | |
| auto msg_type_to_string(msg_type m) { | |
| switch (m) { | |
| MSG_TYPES(MSG_TYPE_TO_STRING) | |
| } | |
| return ""; | |
| } | |
| int main() { | |
| std::tuple<currency, msg_type> t = {currency::EUR, msg_type::reject}; | |
| printf("currency: %s\n", currency_to_string(std::get<currency>(t))); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment