Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Last active August 29, 2015 14:17
Show Gist options
  • Save daniel-j-h/5c7724a0d646904cedc8 to your computer and use it in GitHub Desktop.
Save daniel-j-h/5c7724a0d646904cedc8 to your computer and use it in GitHub Desktop.
SimpleRecord: unscoped enum tuple accessor trick
#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