Coder Social home page Coder Social logo

lambda-tuple's Introduction

lambda-tuple

Coverage

An implementation of std::tuple based on variadic lambda capture (P0780R2) added in C++20. The idea is to store all elements of the tuple in such a lambda:

[... v = std::move(v)](auto f) mutable -> decltype(auto)
{
    return f(v...);
}

I would consider it more of a toy implementation. Nonetheless, there is an extensive test suite including several tests from the Microsoft STL amounting to 75+ tests in total.

Play with it on godbolt.

Advantages

  • Triviallity of copy/move construction of types is preserved.
  • No template or function recursion used in the implementation. (except std::conjunction in requires and noexcept specifiers)
  • Automatic pretty-printing in the debugger. E.g. for tuple<int, double>
    Lambda-tuple pretty-print
  • An empty tuple is std::is_trivial_v.
  • Roughly 2.6 times faster to compile with GCC and 1.9 times faster with Clang. (build-bench)

Disadvantages

  • Cannot be passed across DLL boundaries.
  • No in-place construction, just like std::tuple.
  • Triviallity of copy/move assignment of types is not preserved, just like std::tuple.
  • The compiler is allowed to re-order elements captured in the lambda to minimize its size (by ordering elements by their sizeof or performing empty-class optimization). In practice, however, none of the major compilers seem to make use of it. (godbolt)

Installation

Copy the single header from src/ltpl/tuple.hpp into your project.

Alternatively, use CMake to install the project. From the root of the repository:

cmake -B build -S .
cmake --install build --prefix build/out

And in your CMake project's CMakeLists.txt:

# Add build/out to the CMAKE_PREFIX_PATH
find_package(lambda-tuple)
target_link_libraries(my_app PRIVATE lambda-tuple::lambda-tuple)

Usage

Include the single header:

#include <ltpl/tuple.hpp>

And use ltpl::Tuple just like std::tuple.

Requirements

The only requirement is a small subset of C++20.

The following compilers are continuously tested by Github Actions:

  • GCC 10,
  • Clang 10
  • MSVc 19.32
  • AppleClang 13

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.