{"id":15574461,"url":"https://github.com/giulioromualdi/named_tuple","last_synced_at":"2025-03-29T07:13:04.680Z","repository":{"id":162787009,"uuid":"621764811","full_name":"GiulioRomualdi/named_tuple","owner":"GiulioRomualdi","description":"Single header tagged tuple implementation","archived":false,"fork":false,"pushed_at":"2023-04-06T07:55:01.000Z","size":27,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-03T20:13:55.807Z","etag":null,"topics":["cpp","cpp17","std","tuple"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GiulioRomualdi.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-31T10:37:55.000Z","updated_at":"2023-04-04T10:52:07.000Z","dependencies_parsed_at":"2023-05-28T08:15:16.129Z","dependency_job_id":null,"html_url":"https://github.com/GiulioRomualdi/named_tuple","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiulioRomualdi%2Fnamed_tuple","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiulioRomualdi%2Fnamed_tuple/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiulioRomualdi%2Fnamed_tuple/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiulioRomualdi%2Fnamed_tuple/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GiulioRomualdi","download_url":"https://codeload.github.com/GiulioRomualdi/named_tuple/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246150454,"owners_count":20731419,"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":["cpp","cpp17","std","tuple"],"created_at":"2024-10-02T18:17:12.310Z","updated_at":"2025-03-29T07:13:04.659Z","avatar_url":"https://github.com/GiulioRomualdi.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003ch1 align=\"center\"\u003enamed_tuple \u003c/h1\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://isocpp.org\"\u003e\u003cimg src=\"https://img.shields.io/badge/standard-C++17-blue.svg?style=flat\u0026logo=c%2B%2B\" alt=\"C++ Standard\"/\u003e\u003c/a\u003e\n\u003ca href=\"./LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/license-BSD3-19c2d8.svg\" alt=\"Size\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ch4 align=\"center\"\u003e🍦 Single header tagged tuple implementation 🍦\u003c/h4\u003e\n\u003c/p\u003e\n\n## Rationale behind the library\n\nTo access the content of an [`std::tuple`](https://en.cppreference.com/w/cpp/utility/tuple), we need to use [`std::get\u003c\u003e`](https://en.cppreference.com/w/cpp/utility/tuple/get) . This makes the code complex to read and maintain. \n\nThe following example, taken from  [`std::tuple`](https://en.cppreference.com/w/cpp/utility/tuple), shows how to build and access a `std::tuple` using standard C++.\n```c++\nstd::tuple\u003cdouble, char, std::string\u003e get_student(int id)\n{\n    switch (id)\n    {\n        case 0: return {3.8, 'A', \"Lisa Simpson\"};\n        case 1: return {2.9, 'C', \"Milhouse Van Houten\"};\n        case 2: return {1.7, 'D', \"Ralph Wiggum\"};\n        case 3: return {0.6, 'F', \"Bart Simpson\"};\n    }\n \n    throw std::invalid_argument(\"id\");\n}\n\nint main()\n{\n    const auto student = get_student(0);\n    std::cout \u003c\u003c \"ID: 0, \"\n              \u003c\u003c \"GPA: \" \u003c\u003c std::get\u003c0\u003e(student0) \u003c\u003c \", \"\n              \u003c\u003c \"grade: \" \u003c\u003c std::get\u003c1\u003e(student0) \u003c\u003c \", \"\n              \u003c\u003c \"name: \" \u003c\u003c std::get\u003c2\u003e(student0) \u003c\u003c '\\n';\n}\n```\nAs you can imagine the following code can be complex to debug. Indeed if `std::get\u003c\u003e()` is called with the wrong index, the compiler will not throw any error but the message displayed in the console will be wrong. \n\n**:question: Is there a way to mitigate this?**\n\n`named_tuple` tries to answer this question by implementing a single header library that will allow to access a tuple via string literals and rise a compile-time error in case a wrong string literals is used. \n\n### Basic usage\n\nYou can easily create a `ntuple::named_tuple` with the following code\n\n```c++\n#include \u003cnamed_tuple/named_tuple.h\u003e\n\n// create a named tuple\nusing namespace ntuple::literals;\nauto student0 = ntuple::make_named_tuple(ntuple::named_param\u003c\"GPA\"_h, double\u003e(),\n                                         ntuple::named_param\u003c\"grade\"_h, std::string\u003e(),\n                                         ntuple::named_param\u003c\"name\"_h, std::string\u003e());\n\nntuple::named_tuple\u003cntuple::named_param\u003c\"GPA\"_h, double\u003e(),\n                    ntuple::named_param\u003c\"grade\"_h, std::string\u003e(),\n                    ntuple::named_param\u003c\"name\"_h, std::string\u003e()\u003e student1;\n```\n\nOnce the `ntuple::named_tuple` has been created you can access the elements of the container with the following functions:\n\n```c++\n// get_from_hash\u003c\u003e() allows getting the reference to the parameter via a hash. \n// \"name\"_h returns the hash at compile time.\nstudent0.get_from_hash\u003c\"name\"_h\u003e() = \"Olivia\";\n\n// get\u003c\u003e() extracts the Ith element from the tuple.\n// I must be an integer value in [0, sizeof...(Types))\nstudent1.get\u003c2\u003e() = \"Mark\";\n\n// std::get\u003c\u003e() extracts the Ith element from the tuple.\n// I must be an integer value in [0, sizeof...(Types))\nstd::get\u003c2\u003e(student1) = \"John\";\n```\n\nThe elements of the `ntuple::named_tuple` are accessible also via [Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding) as follows\n\n```c++\nauto\u0026 [GPA, grade, name] = student0;\n```\n\nIn case you need to access to underlined tuple you can call the `to_tuple` method of `ntuple::named_tuple`. Similarly, you can build a `ntuple::named_tuple` from a `std::tuple` by calling the associate constructor or the copy assignment operator.\n\n```c++\n// Create a tuple from named_tuple\nauto temp_tuple = student0.to_tuple();\n\n// this calls the associated constructor.\nntuple::named_tuple\u003cntuple::named_param\u003c\"GPA\"_h, double\u003e(),\n                    ntuple::named_param\u003c\"grade\"_h, std::string\u003e(),\n                    ntuple::named_param\u003c\"name\"_h, std::string\u003e()\u003e student2(temp_tuple);\n\n// this calls the assignment operator\nntuple::named_tuple\u003cntuple::named_param\u003c\"GPA\"_h, double\u003e(),\n                    ntuple::named_param\u003c\"grade\"_h, std::string\u003e(),\n                    ntuple::named_param\u003c\"name\"_h, std::string\u003e()\u003e student3;\nstudent3 = temp_tuple;\n```\n\n:warning: These methods copy the values of `ntuple::named_tuple` into a `std::tuple` and vice-versa.\n\n## Dependences\n\n**`named_tuple`** is a self-contained library,  the only dependency you need is a sufficiently recent C++ compiler (full support to C++17).\n\nIn case you want to compile the tests you need to install [Catch2](https://github.com/catchorg/Catch2).\n\n## Adding `named_tuple` to your project\n`named_tuple` is a single-header file. You can add in three different ways\n\n### Drag and drop\n1. Drop [`named_tuple.h`](./include/named_tuple/named_tuple.h) wherever you like in your source tree\n2. Enjoy `named_tuple.h` 🎉\n\n### Use `FetchContent`\nDrop the following lines in your `CMakeLists.txt`\n```cmake\ninclude(FetchContent)\nFetchContent_Declare(\n    named_tuple\n    GIT_REPOSITORY https://github.com/GiulioRomualdi/named_tuple.git\n    GIT_TAG        v0.1.0\n)\nFetchContent_MakeAvailable(named_tuple)\n```\n\n### Install the repository with `cmake`\nOpen a console and run\n```console\ngit clone https://github.com/GiulioRomualdi/named_tuple.git\ncd name_tuple\nmkdir build \u0026\u0026 cd build\ncmake ../\ncmake --build .\n[sudo] make install\n```\nIf you want to enable tests set the `BUILD_TESTING` option to `ON`.\n\n---\n\nIn case you use `FetchContent` and `cmake`, you can link `named_tuple` by adding the following lines in your `CMakeLists.txt`\n\n```cmake\nproject(foo)\nfind_package(named_tuple REQUIRED)\nadd_executable(${PROJECT_NAME} src/foo.cpp)\ntarget_link_libraries(${PROJECT_NAME} named_tuple::named_tuple)\n```\n\n## Bug reports and support\n\nAll types of [issues](https://github.com/GiulioRomualdi/named_tuple/issues/new) are welcome.\n\n## Note\nThis library took inspiration from https://vitiy.info/named-tuple-for-cplusplus/. Moreover `named_tuple` is Here some other projects you may be interested to follow:\n\n- Discussions on [StackOverflow](http://stackoverflow.com/questions/13065166/c11-tagged-tuple) and [GoogleGroups](https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/N-kIXNrkTUk)\n- [Inline Object Declaration](https://github.com/matt-42/iod)\n- [named_types](https://github.com/duckie/named_types)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiulioromualdi%2Fnamed_tuple","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiulioromualdi%2Fnamed_tuple","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiulioromualdi%2Fnamed_tuple/lists"}