GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - basicpp/result17: A rust like Result type for modern C++

BranchesTags
Open more actions menu

Latest commit

 

History

17 Commits
17 Commits

Folders and files

NameName
Last commit message
Last commit date
src/result17.lib
src/result17.lib
 
 
thirdparty
thirdparty
 
 
.clang-format
.clang-format
 
 
.clang-tidy
.clang-tidy
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
result.qbs
result.qbs
 
 

Repository files navigation

C++17 Result

Build Status

A Rust inspired Result<V,E> type for C++17.

template<typename V, typename E>
struct Result;

basically a sophisticated wrapper for:

std::variant< ok<V>, error<E> >

Usage synopsis

auto fn() -> Result<std::ifstream, string>;
auto splitLines(std::stringstream&) -> std::vector<string>;

int main() {
    auto f = openFile("hello.txt")
        .orMap([](auto&&) { return openFile("fallback.txt"); }) // invoked only on error
        .andMap([](auto&& in) -> Result<std::stringstream, string> { // invoked only if successful
            std::stringstream out;
            out << in.rdbuf();
            if (out.str().empty()) return error("empty file"s);
            return ok(move(out));
        })
        .andMap(&splitLines) // adds no errors
        .orMap([](auto) -> Result<std::vector<string>, string> {
            return ok(std::vector<string>{});
        });
    for (auto &l : f.unwrap()) std::cout << l << '\n'; // we are sure we have a value
}

see Result.test.cpp for a working example.

Requirements

You will need a C++17 compiler.

  • GCC 8.x
  • Clang 8.x + libc++
  • MSVC2017/2019
  • googletest is optional to run the tests

Links

  • github/oktal/result a similar result type for C++14
  • doc.rust-lang/std/result the rust result type documentation

Status

Even though this seems to work, it is only a proof of concept.

  • it works
  • std::variant is unusable slow for this kind of use case.
  • std::reference_wrapper does not allow comparisons.

About

A rust like Result type for modern C++

Topics

cpp17error-handlingmonadresult-type

Resources

Readme
MIT license
Activity
Custom properties

Stars

20 stars

Watchers

7 watching

Forks

4 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.