{"id":29192592,"url":"https://github.com/johnjohnlin/namedtuple","last_synced_at":"2026-03-09T17:12:20.636Z","repository":{"id":65609775,"uuid":"588229277","full_name":"johnjohnlin/namedtuple","owner":"johnjohnlin","description":"Implementation of super-fast C++-styled namedtuple, for compile-time reflection.","archived":false,"fork":false,"pushed_at":"2023-02-05T08:46:40.000Z","size":84,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-28T02:42:29.389Z","etag":null,"topics":["c-plus-plus","cpp","cpp11","cpp14","cpp17","header-only","reflection"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/johnjohnlin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-12T16:24:15.000Z","updated_at":"2025-03-14T16:21:05.000Z","dependencies_parsed_at":"2023-02-16T20:41:00.942Z","dependency_job_id":null,"html_url":"https://github.com/johnjohnlin/namedtuple","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/johnjohnlin/namedtuple","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnjohnlin%2Fnamedtuple","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnjohnlin%2Fnamedtuple/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnjohnlin%2Fnamedtuple/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnjohnlin%2Fnamedtuple/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johnjohnlin","download_url":"https://codeload.github.com/johnjohnlin/namedtuple/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnjohnlin%2Fnamedtuple/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263061334,"owners_count":23407600,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-plus-plus","cpp","cpp11","cpp14","cpp17","header-only","reflection"],"created_at":"2025-07-02T01:37:31.057Z","updated_at":"2026-03-09T17:12:20.592Z","avatar_url":"https://github.com/johnjohnlin.png","language":"C++","readme":"[Build status](https://github.com/johnjohnlin/namedtuple/actions/workflows/unittest.yaml/badge.svg)\n\n# What is namedtuple?\n`namdetuple` is a super fast C++ type-reflection library (5x `BOOST_HANA_DEFINE_STRUCT`),\nand you can insert the `namedtuple` macro into your existing structure without modification.\nIt's based on `boost.preprocessor`, and the core of our macro is only roughly 10 lines.\n\n\n# Idea behind namedtuple\nIn Python, there is a `namedtuple`\n\n```python\nPoint = namedtuple('Point', ['x', 'y'])\np = Point(11, y=22)\np[0] + p[1] # 33\np.x + p.y # 33\n```\n\nIn C++, we have `tuple` and struct, but we cannot connect them easily.\n\n```cpp\nstruct S { int x; float y; string z; };\nS s_struct;\ntuple\u003cint, float, string\u003e s_tuple;\ns_struct.y; // float\ns_tuple.get\u003c1\u003e(); // float\ns_tuple.y; // ERROR\ns_struct.get\u003c1\u003e(); // ERROR\n```\n\nWe provide a macro for converting your struct to a `namedtuple` in C++ easily.\n\n```cpp\nstruct S2 {\n\tint    x;\n\tfloat  y;\n\tstring z;\n\t// NOTE: no semicolon here\n\tMAKE_NAMEDTUPLE(x, y, z)\n};\nS2 s_namedtuple;\ns_namedtuple.y; // float\ns_namedtuple.get\u003c1\u003e(); // float, the reference of y\nstatic_assert(sizeof(S2) == sizeof(S)); // namedtuple does not add extra members!\nstatic_assert(sizeof(S2::num_members) == 3u); // namedtuple also provides ::num_members\nS2::get_name(1); // const char* = \"y\"\n```\n\n# nametuple: why and how?\n`namedtuple` provides similar functionalities as [boost::hana](https://boostorg.github.io/hana/index.html) for struct meta-programming (specifically, `BOOST_HANA_DEFINE_STRUCT`).\nYou can loop over the member of a struct in compile time, so you can implement JSON-like-`cout` struct without repeatedly writing boring code.\nBesides, the core of `namedtuple` is very simple (roughly 10 lines of macro!), so it has super fast compilation time.\nNext we shall show some `namedtuple` usages and review the `namedtuple` performance.\n\n## Sample usage\nWith the `get()` and its overloaded functions, we can make use of parameter pack (C++11) of fold expression (C++17).\nFor example, if we want to compare two namedtuples, but ignore members started with an underscore:\n\n```cpp\nstruct S2 {\n\tint    x;\n\t// will not compare member started with \"_\"\n\tfloat  _y;\n\tstring z;\n\tMAKE_NAMEDTUPLE(x, y, z)\n};\n\ntemplate\u003ctypename NT, unsigned...indices\u003e\nbool compare_impl(const NT\u0026 lhs, const NT\u0026 rhs, integer_sequence\u003cunsigned, indices...\u003e) {\n\treturn (\n\t\t(\n\t\t\tNT::template get_name\u003cindices\u003e()[0] == '_' or\n\t\t\tlhs.template get\u003cindices\u003e() == rhs.template get\u003cindices\u003e()\n\t\t)\n\t\tand ...\n\t);\n}\n\ntemplate\u003ctypename NT\u003e\nbool compare(const NT\u0026 lhs, const NT\u0026 rhs) {\n\treturn compare_impl(lhs, rhs, make_integer_sequence\u003cunsigned, NT::num_members\u003e{});\n}\n\nS2 a{1, 2, \"3\"};\nS2 b{1, 3, \"3\"};\ncompare(a, b); // true\nb.z = \"\";\ncompare(a, b); // false\n```\n\nWith `boost::hana`, you can achieve the same thing, but `namedtuple` is much faster.\nWe shall show this in the following section.\n\n## Compile time performance compared with boost::hana\n\n`boost::hana` is one of the most efficient template library as shown [here](https://boostorg.github.io/hana/index.html#tutorial-performance).\n`namedtuple` is much faster than `boost::hana` due to its simplicity.\nWe compare three versions of implementation of `ostream\u003c\u003c` with tens of classes in one file:\n* Use `for_each` in `hana` to implement generic `ostream\u003c\u003c`\n* Use `foreach` in `namedtuple` to implement generic `ostream\u003c\u003c`\n* Use code generator to generate plain `ostream\u003c\u003c`\n\nIn this specific scenario, we consistently save time/memory by a factor of 5 compared with `boost::hana`, only costing 11-50% overhead compared to non-templated C++ code with code-gen.\n\nTesting scenario:\n\n* Randomly generate tens of deeply nested struct\n* ArchLinux 202301\n* AMD 3700X\n* gcc 12.2.1\n* No extra compile flag\n* Benchmark under `benchmarks/benchmark1`\n\n### Peak memory usage (KB)\n| #structs | Hana | namedtuple | Code-gen | Improvement vs hana |\n| -: | -: | -: | -: | -: |\n| 20 | 499960 | 125364 | 80528 | 3.99x |\n| 25 | 573604 | 138156 | 84188 | 4.15x |\n| 30 | 720840 | 152108 | 88040 | 4.74x |\n| 35 | 826164 | 165412 | 91512 | 4.99x |\n| 40 | 831900 | 179880 | 96728 | 4.62x |\n\n### Compile time (second)\n| #structs | Hana | namedtuple | Code-gen | Improvement vs hana |\n| -: | -: | -: | -: | -: |\n| 20 | 6.81  | 1.02 | 0.67 | 6.67x |\n| 25 | 12.11 | 2.12 | 1.74 | 5.71x |\n| 30 | 20.59 | 4.10 | 3.55 | 5.02x |\n| 35 | 25.96 | 5.47 | 4.92 | 5.27x |\n| 40 | 37.02 | 8.31 | 7.44 | 4.45x |\n\n# Implementations\n\nThe idea is simple. If we implement `get(X)`, where X is overloaded with different `integral_constant`, then we can implement `get\u003cunsigned\u003e()` easily. This is made possible by `boost.preprocessor`.\n\n```cpp\nstruct S2 {\n\tint    x;\n\tfloat  y;\n\tstring z;\n\t// extra code inserted by MAKE_NAMEDTUPLE(x, y, z)\n\t// implemented by boost.preprocessor\n\tint    get(integral_constant\u003cunsigned, 0\u003e) { return x; }\n\tfloat  get(integral_constant\u003cunsigned, 1\u003e) { return y; }\n\tstring get(integral_constant\u003cunsigned, 2\u003e) { return z; }\n\ttemplate\u003cunsigned x\u003e auto get() { return get(integral_constant\u003cunsigned, x\u003e()); }\n};\n```\n\n# Requirements\nJust copy everything under `include/` to anywhere you can include.\nThe core `namedtuple` is macro based on `boost.preprocessor` (you don't need the entire boost), so it only requires C++11.\nHowever, `util/` might require C++14 or C++17 features, or specifically, auto template, `integer_sequence` and fold expressions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnjohnlin%2Fnamedtuple","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnjohnlin%2Fnamedtuple","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnjohnlin%2Fnamedtuple/lists"}