Giter VIP home page Giter VIP logo

better_option's Introduction

Better Option

This is a small header only library with implementation of C++17 std::optional

Main differences

  1. It supports references!. Via custom Ref wrapper. Similar to std::reference_wrapper but simplier
  2. It has niche optimization for references: sizeof(Option<Ref<T>>) == sizeof(T*)
  3. It supports void via custom Void type. We can .map with functions returning void and also Option<Void> can be mapped with function accepting no arguments
  4. C++20.
using better::None;
using better::Option;
using better::Ref;
using better::Some;

int main() {

    Option<std::string> opt = {Some, "hello world"};

    auto transformed = std::move(opt)
                           .map([](auto &&str) { return str.length(); })
                           .map([](size_t len) { return std::to_string(len); });

    // can map to void!
    auto result = transformed.as_ref().map(
        [](const auto &str) { std::cout << *str << "\n"; });

    // we have niche optimization for references
    static_assert(sizeof(transformed.as_ref()) == sizeof(void*));

    std::cout << "transformed is stil some: " << transformed.is_some() << "\n";

    std::string world = "world";

    std::cout << result.is_some() << "\n";
    static_assert(sizeof(result) == sizeof(bool), "Option<Void> is one byte only");

    // can also map from void!
    std::move(result)
        .map([&]() -> std::string & { return world; })
        .map([](auto ref) { std::cout << "got ref: " << *ref << "\n"; });

    return 0;
}

better_option's People

Contributors

nekrolm avatar

Stargazers

Sargis avatar

Watchers

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