GithubHelp home page GithubHelp logo

gregthemadmonk / untup Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 12 KB

Flatten nested tuple-like objects in your C++20 code as much as you want!

License: MIT License

CMake 14.83% C++ 85.17%
header-only modules tuples fatten

untup's Introduction

UnTup

A small C++20 library to flatten nested tuple-like objects into a single tuple of copies or references.

also known as

A sliiightly overengineered solution to a simple problem of having a single structured binding in a for loop that uses std::views::zip and std::views::enumerate.

Does

What is claims to do (no warranty though), constexpr-enabled. By default, performs some tests in compile-time, too.

Includes

  • untup::untie() - converts nested tuple-like objects into a tuple of references:
auto t = std::tuple{ std::pair{ 'c', 3.1415 }, 2 };
auto [ c, pi, two ] = untup::untie(t);
pi = 3.0; // t is changed!
  • untup::untup() - converts nested tuple-like objects into a tuple of copies:
auto t = std::tuple{ std::pair{ 'c', 3.1415 }, 2 };
auto [ c, pi, two ] = untup::untup(t);
pi = 3.0; // Ehm.. OK? `t` doesn't care
  • untup::views::flatten - a shortcut to std::views::transform that calls untup::untup() on the element of the range:
for (
    auto [ idx, e1, e2 ] : std::views::zip(v1, v2)
                           | std::views::enumerate
                           | untup::views::flatten
) println("{}: {} and {}", idx, e1, e2);
// Instead of
for (
    auto [ idx, pair ] : std::views::zip(v1, v2)
                         | std::views::enumerate
) {
    auto [ e1, e2 ] = pair; // Eww, why?
    println("{}: {} and {}", idx, e1, e2);
}

Preserves

Reference types when copying with untup::untup()!

Requires

At least C++20.

Known issues

  • Doesn't build as a module on GCC.
  • Headers in module global fragment could cause errors. Read how to fix that below.

Adding to your project

git clone this repo into your project. Add

set( UT_HEADERONLY ON ) # Only if you don't want the module
add_subdirectory( path/to/UnTup )

target_link_libraries( YourTarget UnTup ) # Or UnTup::UnTup

to your CMakeLists.txt. Use with #include <untup.hh> or import untup;.

Headers in global module fragment break the build!

Yeah I know. This is why you can specify CXX_STD_MODULE and CXX_STD_MODULE_TARGET to provide a name of your module that provides a standard library to your project. If you're using modules, you probably have it anyway. UnTup will import CXX_STD_MODULE; instead of including STL headers and If CXX_STD_MODULE_TARGET is set, CMake will link UnTup to it making sure the target under this name is built before it.

Alternatively

Just copy untup.hh somewhere in your project and #include it.

untup's People

Stargazers

 avatar  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.