Best way to return error codes from a function in c++ ?

Best way to return error codes from a function in c++ ?

The "anything but 0 is an error" seems a bit niggerlicious to me

Attached: 1884a41d868e060b26da6f78dd6920743c4d2e873431c59900917ca9785739d7.jpg (603x324, 105K)

>The "anything but 0 is an error" seems a bit niggerlicious to me
Why? That's the simplest and most straightforward thing you can do. You can use an enum to be clearer about it. Then again if you're writing C++ I suppose you've sacrificed being simple and straightforward.

that's what enums are for, give everything a meaningful name

use exceptions nigga

I do like that it makes checking for no error / any error really easy, but i dont like how it leaves you with 0 information about the exact error and pretty much forces you to go lookup what error enum that specific funciton uses.

For example in the win32 socket api, some functions return error codes from one enum while other functions return error codes from another enum.

Do not listen to this retard no one uses exceptions on C++

std::optional :)

try reading through the documentation, nigger.

>niggerlicious
Why you think cia scriptfags hate c++?

Just return 1, 2, 3 with every different error type you can send back. Then run a check to see if it's not 0 - if it isn't, then parse the returned error code. Not difficult dude come on

c++17

Attached: 1562536805299.png (1000x700, 101K)

Whats wrong with c++ 17?
Honest question.

He uses a shitdistro with a compiler too old to have -std=c++17

t. seething Ccuck

That is exactly what i dont like about that solution. You HAVE to lookup, in the documentation, what errors correspond to what codes. And on top of that if two functions don't use the same "error tables" and the programmer assumes they do, then they'll get some pretty annoying bugs.

we'll i just dont want incompatibility with older c++ versions. Many codebases still use c++14 / c++11

Well not a problem if you need something from scratch.

Even i get annoying jobs where one device has 32bit dll and has to work with another device that has 64bit dll.

Return an int and use an error buffer you mongoliod

>Best way to return error codes from a function in c++ ?
Throw an exception, jesus.

Autism

enum class Error { Faggotry, IO, BadParam };
std::option compute_OP_powerlevel(); // always returns Error::Faggotry

so many ways, just use the one that makes more sense for your API.

>exceptions
>return error code
>return std::optional
>return nullptr
>write error on variable passed by reference or pointer

thats the beauty of this language, its all about the programmers choice.

fn read_username_from_file() -> Result {
let mut f = File::open("username.txt")?;
let mut s = String::new();

f.read_to_string(&mut s)?;

Ok(s)
}

>Doesn't read the documentation
You could always just
return "Error: Invalid argument!";

>leaves you with 0 information about the exact error
Read docs dumbass

Use Boost.

>rust tranny

Use Lisp.

>std::optional gender;

Attached: deformed reee.png (680x680, 290K)

return -1

one thing that annoys me is that this approach differs when functions return pointers. So a zero is failure, whereas elsewhere zero is success. Functions should return zero on failure and one on success for consistency.

Maybe a

Returning error codes isn't so bad. You can also throw exceptions or return optionals/variants depending on the circumstances.

Just remember to annotate your functions with nodiscard or noexcept.

>not std::variant

this is true and also the reason why most C++ software is garbage
t. C++ dev

the convention in C is that negatives are error and you check for res < 0
if there is just one meaning for error, you return -1, otherwise make an enum and return -ENUM_ENTRY
for success values, 0 is generic ok

return const char *ErrorString
There, you happy?

eastl::optional then

/thread

the
whole
point
of reserving all numbers but 0 for errors is so you CAN deliver error info in the return code

plz be bait

opengl and windows have internal error flags u querry them with glgeterror() and geterror()
thats good if perfm matters to you but
std:optinal is retard for fast code its sizeof(std::optinal ) > sizeof(T) ezz retard way. just returning error state is retarded if u think pure function wise mutable& or ptr suck as argument i prefer exception but they come at high cost if you throw all time very bad practice best is error free pure function in loops dont give your functions errors.

panic("error message");
:^)

static_assert(!rust_is_cancer,"error_message");

rust?

t. retard

Why throw at runtime, when u can run your code at compiletime?
Why crash at runtime, when trannies love to check in compiletime!

Result :)

what is the book for the latest c++ version anyway? i got one but i think its not the latest

>no one uses exceptions on C++
Only cancer fags avoid exceptions.

the sucker for punishment side of me wants to duplicate rust's Result in c++

but the sane side of me wonders why the fuck i'd bother when i barely use c++ now

*VBA flashbacks intensify*