Skip to content

cshmookler/cpp_result

Repository files navigation

cpp_result

A compromise between error codes and exceptions. Contains result types with detailed error messages and stack traces.

Error codes are the old-school method for error handling. Error codes are more verbose than exceptions and require the programmer to explicitly handle all points of failure. Error messages corresponding to error codes are too general and not particularly descriptive.

Exceptions generate more descriptive error messages than error codes, but they're not without problems. Exceptions break code structure by creating multiple invisible exit points that make code hard to read and inspect. Exception safety requires RAII, which is not always guaranteed, especially when using C libraries in C++. Exceptions thrown while modifying invariants (variables with only a few valid states) can result in undefined behavior, regardless of whether the exceptions are caught. Writing exception-safe code in C++ is hard.

Result types are improved error codes with exception-like error messages. As with error codes, result types must be explicitly handled at all points of failure. As with exceptions, result types contain stack traces and highly descriptive error messages.

Build from Source

1.  Install a C++ compiler and Meson.

Windows:

Install ONE of the following C++ compilers:

  • Visual Studio's C++ compiler (MSVC)
    • Select the "Desktop development with C++" option.
  • MinGW (GCC for Windows)
    • Mark "mingw32-gcc-g++" for installation. Then select "Apply Changes" within the "Installation" dropdown.
    • Add the MinGW bin directory (C:\MinGW\bin\) to your PATH.

Install Meson (C++ meta-build system): - Download and execute the Windows installer (.msi file) from the official repository.

Mac:

Install Homebrew (package manager for Mac) by opening a terminal and entering the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Use Homebrew to install GCC (C++ compiler):

brew install gcc

Follow these instructions to install Meson (C++ meta-build system).

Linux (Debian/Ubuntu):

sudo apt install meson ninja build-essential

Linux (Arch):

sudo pacman -S git meson base-devel

2.  Clone this project.

This project can be downloaded online here.

Alternatively, if you have Git installed, open command prompt (Windows) or a shell (Linux & Mac) and enter the commands below. This project will be downloaded to the current working directory.

git clone https://github.com/cshmookler/cpp_result.git
cd cpp_result

3.  Build this project from source.

meson setup build
cd build
ninja

4.  (Optional) Install this project globally.

meson install

TODO

  • Create a dedicated error type to distinguish between strings and errors.
  • Create a result type that indicates success or failure.
  • Create an optional type that either contains a value or an error.
  • Add tests for all methods.
  • Add examples for all types.
  • Add Conan integration.

About

A compromise between error codes and exceptions. Contains result types with detailed error messages and stack traces.

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Packages

 
 
 

Contributors