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

Robert James
Robert James

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 (105 KB, 603x324)

David Lopez
David Lopez

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.

Hunter Allen
Hunter Allen

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

Christopher Diaz
Christopher Diaz

use exceptions nigga

Anthony Thomas
Anthony Thomas

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.

Matthew Taylor
Matthew Taylor

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

Nicholas Kelly
Nicholas Kelly

std::optional :)

Isaac Jones
Isaac Jones

try reading through the documentation, nigger.

Adam Sanchez
Adam Sanchez

niggerlicious
Why you think cia scriptfags hate c++?

Nathaniel Reyes
Nathaniel Reyes

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

Jacob Thompson
Jacob Thompson

c++17

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

Asher Anderson
Asher Anderson

Whats wrong with c++ 17?
Honest question.

Xavier Morales
Xavier Morales

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

Daniel Parker
Daniel Parker

t. seething Ccuck

Jack Lewis
Jack Lewis

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.

Jordan Baker
Jordan Baker

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

Jose Taylor
Jose Taylor

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.

Levi Johnson
Levi Johnson

Return an int and use an error buffer you mongoliod

Evan Reed
Evan Reed

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

Autism

Elijah Smith
Elijah Smith

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

Joshua Robinson
Joshua Robinson

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.

Hudson Walker
Hudson Walker

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

f.read_to_string(&mut s)?;

Ok(s)
}

James Reed
James Reed

Doesn't read the documentation
You could always just

 return "Error: Invalid argument!"; 

Jordan Roberts
Jordan Roberts

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

Cooper King
Cooper King

Use Boost.

Kevin Carter
Kevin Carter

rust tranny

Andrew Lopez
Andrew Lopez

Use Lisp.

Luis Hill
Luis Hill

std::optional<std::any> gender;

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

Jack Perez
Jack Perez

return -1

Adam Cook
Adam Cook

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.

Samuel Fisher
Samuel Fisher

Maybe a

Isaac Bennett
Isaac Bennett

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.

John Kelly
John Kelly

not std::variant<Result, Error>

Austin Perry
Austin Perry

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

Aiden Morgan
Aiden Morgan

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

Liam Baker
Liam Baker

return const char *ErrorString
There, you happy?

Joshua Price
Joshua Price

eastl::optional then

Parker Campbell
Parker Campbell

/thread

Noah Miller
Noah Miller

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

Julian Ramirez
Julian Ramirez

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<T> ) > 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.

Michael Rogers
Michael Rogers

 panic("error message"); 

:^)

Hunter Walker
Hunter Walker

static_assert(!rust_is_cancer,"error_message");

Asher Stewart
Asher Stewart

rust?

Eli Cook
Eli Cook

t. retard

Charles Watson
Charles Watson

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

Luke Lopez
Luke Lopez

Result<T, E> :)

Benjamin Phillips
Benjamin Phillips

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

John Long
John Long

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

Justin Lee
Justin Lee

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

Benjamin Wright
Benjamin Wright

*VBA flashbacks intensify*

Disable AdBlock to view this page

Disable AdBlock to view this page

Confirm your age

This website may contain content of an adult nature. If you are under the age of 18, if such content offends you or if it is illegal to view such content in your community, please EXIT.

Enter Exit

About Privacy

We use cookies to personalize content and ads, to provide social media features and to analyze our traffic. We also share information about your use of our site with our advertising and analytics partners.

Accept Exit