Coder Social home page Coder Social logo

willkill07 / named-tuple Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 8 KB

License: Creative Commons Zero v1.0 Universal

CMake 21.20% C++ 78.80%
cpp cpp-lib cpp-library cpp-templates cpp20 cpp20-lib cpp20-library header-only header-only-lib header-only-library

named-tuple's Introduction

named-tuple

Single header implementation a named-tuple in C++

Tuples have always been addressible via index and type -- the obvious gap here is accessing by name. This (small) header-only library provides just that!

Usage

auto make_config() {
    using Configuration = named_tuple<
      named_val<std::string, "install-path">,
      named_val<std::string, "build-path">,
      named_val<int, "retry-limit">,
      named_val<bool, "system-wide">
    >;

    Configuration default_config{"/usr/local/", "./build", 1, true};
    return default_config;
}

void use_config(auto&& config) {
    // named_get supports multiple values and will construct an intermediate named_tuple
    auto build_options = named_get<"build-path", "retry-limit">;
    auto install_options = named_get<"install-path", "system-wide">;
}

Components

  • named_tuple: The tuple class - sizeof(named_tuple) == sizeof(tuple)
  • named_val: descriptor used to indicate the type and name
  • named_get<"name">: accessor for retrieving a value stored in the named_tuple
  • named_get<names...>: accessor for splicing a named_tuple. You can even permute!
  • named_tie<names...>: std::tie() equivalent for named_tuple

Limitations

  • Deduction guides are not really feasible due to their limitation of needing all template arguments specified.
  • I have not extensively tested the reference semantics and behavior. I welcome contributions to this library.

Compiler support

Requires a small subset of C++20 features.

Tested on:

  • Linux
    • g++ 13.0
    • clang 16.0
  • macOS
    • g++ 13.0
    • clang 16.0
    • AppleClang 14.0

CC0

To the extent possible under law, Will Killian has waived all copyright and related or neighboring rights to the expected library. This work is published from: the United States of America.

named-tuple's People

Contributors

willkill07 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.