Skip to content

Instantly share code, notes, and snippets.

@BB-301
Last active December 3, 2024 12:38
Show Gist options
  • Save BB-301/2f8855a8cc2fe07e38ef4eaa690c6fa4 to your computer and use it in GitHub Desktop.
Save BB-301/2f8855a8cc2fe07e38ef4eaa690c6fa4 to your computer and use it in GitHub Desktop.
Lightweight C++ Result Type with Concepts and Free Function Utilities

Introduction

This Gist demonstrates a simple yet powerful implementation of a Result type in modern C++, inspired by similar constructs in Rust and Swift. The Result type is designed to encapsulate either a success value or an error value, providing a type-safe way to handle operations that can fail.

Using C++20 features like std::variant and concepts, this implementation enforces compile-time constraints, ensuring that the types used in Result meet specific requirements (e.g., streamability). All functionality is encapsulated in a namespace with free function utilities, following the idiomatic C++ style found in the Standard Library.

Our goal was to explore different approaches for implementing and using a Result type, balancing object-oriented and functional programming paradigms. While we ultimately chose the free function approach for its flexibility, reusability, and alignment with modern C++ practices, we also include the class-based version we studied earlier as an extra. This alternative emphasizes encapsulation and a method-based API, providing a contrasting perspective for readers interested in different design philosophies.

Both approaches offer valuable insights into designing modern C++ utilities, and we hope this Gist inspires further exploration and discussion!

/*
Copyright (c) 2024 BB-301 <fw3dg3@gmail.com>
[GitHub Gist](https://gist.github.com/BB-301/2f8855a8cc2fe07e38ef4eaa690c6fa4)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <format>
#include <iostream>
#include <string>
#include <variant>
namespace result_utils
{
template <typename T>
concept streamable = requires(std::ostream &os, const T &value) {
{
os << value
} -> std::same_as<std::ostream &>;
};
template <typename T, typename E>
requires streamable<T> && streamable<E>
using result = std::variant<T, E>;
template <typename T, typename E>
bool is_success(const result<T, E> &res)
{
return std::holds_alternative<T>(res);
}
template <typename T, typename E>
bool is_error(const result<T, E> &res)
{
return std::holds_alternative<E>(res);
}
template <typename T, typename E>
const T &get_success(const result<T, E> &res)
{
if (!is_success(res))
{
throw std::runtime_error("Result does not contain a success value.");
}
return std::get<T>(res);
}
template <typename T, typename E>
const E &get_error(const result<T, E> &res)
{
if (!is_error(res))
{
throw std::runtime_error("Result does not contain an error value.");
}
return std::get<E>(res);
}
template <typename T, typename E, typename SuccessFn, typename ErrorFn>
auto match(const result<T, E> &res, SuccessFn success_fn, ErrorFn error_fn)
{
if (is_success(res))
{
return success_fn(get_success(res));
}
else
{
return error_fn(get_error(res));
}
}
template <typename T, typename E>
std::ostream &operator<<(std::ostream &os, const result<T, E> &res)
{
if (is_success(res))
{
os << "Success: " << get_success(res);
}
else
{
os << "Error: " << get_error(res);
}
return os;
}
}
struct MySuccess
{
std::string message;
friend std::ostream &operator<<(std::ostream &os, const MySuccess &self)
{
return os << std::format("Success {{ .message = {} }}", self.message);
}
};
enum class MyError
{
unknown,
invalid_argument
};
std::ostream &operator<<(std::ostream &os, const MyError &self)
{
switch (self)
{
case MyError::invalid_argument:
return os << "Error { Invalid argument }";
case MyError::unknown:
return os << "Error { Unknown }";
}
return os;
}
int main()
{
using namespace result_utils;
result<MySuccess, MyError> res1 = MySuccess{"Operation completed successfully"};
result<MySuccess, MyError> res2 = MyError::invalid_argument;
std::cout << res1 << '\n';
std::cout << res2 << '\n';
match(
res1,
[](const MySuccess &s)
{ std::cout << "Matched success: " << s << '\n'; },
[](const MyError &e)
{ std::cout << "Matched error: " << e << '\n'; });
match(
res2,
[](const MySuccess &s)
{ std::cout << "Matched success: " << s << '\n'; },
[](const MyError &e)
{ std::cout << "Matched error: " << e << '\n'; });
return 0;
}
/*
Copyright (c) 2024 BB-301 <fw3dg3@gmail.com>
[GitHub Gist](https://gist.github.com/BB-301/2f8855a8cc2fe07e38ef4eaa690c6fa4)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <format>
#include <iostream>
#include <string>
#include <variant>
namespace result_utils
{
template <typename T>
concept Streamable = requires(std::ostream &os, const T &value) {
{
os << value
} -> std::same_as<std::ostream &>;
};
template <typename T, typename E>
requires Streamable<T> && Streamable<E>
class Result
{
private:
std::variant<T, E> value;
public:
Result(T success)
: value(std::move(success)) {}
Result(E error)
: value(std::move(error)) {}
bool is_success() const
{
return std::holds_alternative<T>(value);
}
bool is_error() const
{
return std::holds_alternative<E>(value);
}
const T &get_success() const
{
if (!is_success())
throw std::runtime_error("Not a success value");
return std::get<T>(value);
}
const E &get_error() const
{
if (!is_error())
throw std::runtime_error("Not an error value");
return std::get<E>(value);
}
template <typename SuccessFn, typename ErrorFn>
auto match(SuccessFn successFn, ErrorFn errorFn) const
{
if (is_success())
return successFn(std::get<T>(value));
return errorFn(std::get<E>(value));
}
friend std::ostream &operator<<(std::ostream &os, const Result &result)
{
if (result.is_success())
{
return os << "Success: " << result.get_success();
}
else
{
return os << "Error: " << result.get_error();
}
}
};
}
struct MySuccess
{
std::string message;
friend std::ostream &operator<<(std::ostream &os, const MySuccess &self)
{
return os << std::format("Success {{ .message = {} }}", self.message);
}
};
enum class MyError
{
Unknown,
InvalidArgument
};
std::ostream &operator<<(std::ostream &os, const MyError &self)
{
switch (self)
{
case MyError::InvalidArgument:
return os << "Error { Invalid argument }";
case MyError::Unknown:
return os << "Error { Unknown }";
}
return os;
}
int main()
{
using namespace result_utils;
Result<MySuccess, MyError> result_1 = MySuccess{"This is a message"};
Result<MySuccess, MyError> result_2 = MyError::InvalidArgument;
std::cout << result_1 << '\n';
std::cout << result_2 << '\n';
result_1.match([](const MySuccess &s)
{ std::cout << "Matched success: " << s << '\n'; },
[](const MyError &e)
{ std::cout << "Matched error: " << e << '\n'; });
result_2.match([](const MySuccess &s)
{ std::cout << "Matched success: " << s << '\n'; },
[](const MyError &e)
{ std::cout << "Matched error: " << e << '\n'; });
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment