Last active
August 29, 2015 14:17
-
-
Save daniel-j-h/5c7724a0d646904cedc8 to your computer and use it in GitHub Desktop.
SimpleRecord: unscoped enum tuple accessor trick
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> | |
#include <string> | |
#include <tuple> | |
// See: EMC++, Item 10 | |
namespace { | |
using Person = std::tuple<std::string, std::string, int>; | |
enum { FirstName, LastName, Age }; | |
} | |
int main() { | |
Person p{"Erik", "Eriksson", 25}; | |
using std::get; | |
std::cout << get<FirstName>(p) << ' ' << get<LastName>(p) << std::endl; | |
// instead of get<0>(p), get<1>(p) | |
} | |
/* I guess this could be abused for: | |
#define Record(...) std::make_tuple(__VA_ARGS__) | |
#define Accessors(_, ...) enum {__VA_ARGS__} | |
auto q = Record("Erik","Eriksson"); | |
Accessors(q, First, Last); | |
std::cout << get<First>(q) << ' ' << get<Last>(q) << std::endl; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment