https://github.com/hellozee/errors
An idiot's attempt to do a go like error handling in c++
https://github.com/hellozee/errors
cmake cpp error-handling errors exception-handling exceptions golang
Last synced: 9 months ago
JSON representation
An idiot's attempt to do a go like error handling in c++
- Host: GitHub
- URL: https://github.com/hellozee/errors
- Owner: hellozee
- License: gpl-2.0
- Created: 2019-06-30T19:36:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-27T08:23:23.000Z (over 3 years ago)
- Last Synced: 2024-04-21T11:28:41.371Z (over 1 year ago)
- Topics: cmake, cpp, error-handling, errors, exception-handling, exceptions, golang
- Language: CMake
- Homepage:
- Size: 43 KB
- Stars: 12
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# If you are looking at this beware there are better alternatives like std::expected or if your C++ standard doesn't provide that there is [tl::expected](https://github.com/TartanLlama/expected)
# errors
An idiot's attempt to do a `go` like error handling in `c++`
Really simple library and obviously the code is self documenting, take a look at the examples if you want to use.
Unfortunately there is no install rule but can be used as git submodule in a `cmake` project.
### What inspired me?
Along with `c++`, I also use `go` at least for my personal projects. And I really like how `go` handles errors without the use of any exceptions or such. How does it do that? Simple by using multiple returns, one of which is the error and then we check if the error is nil or not. Like this,
```go
f, err := os.Open("filename.ext")
if err != nil {
//oops error
}
```
I try to imitate the same thing in `c++`, for example I can write the above example using my library as
```c++
#include
..
..
errors::container result = some_function(param);
if(result.err() != errors::nil()){
//oops error
}
```
The library is a header only library.
I try to avoid involving `std::exception` in the library, as well as the resulting code, it is being used in, but I am not sure if I am able to get that, would be happy if someone could point me out an example where exceptions would involved and if possible can suggest a solution. :smile: