Convention result and code error C++ int foo (…)

2019-07-15 16:51发布

In Linux for example when i use batch if error code is 0 thats good, but what is the convention in C++ ?

when int (or bool) is equal to one we say that's true, but what must be the return of such function in C++ ?

5条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-15 16:52

In C and its runtime library 0 means success (sometimes polling errno is required).

In C++ world is more colourful:

  • bool for success/failure
  • enumeration if there are more states
  • exceptions for ... well exceptional cases and object construction/destruction; sometimes they are not available or allowed (mostly embedded platforms, sometimes games to maximize performance)

DON'T mix bool usage convention and "0", result is just plain confusing code(see example). If you happen to need multiple values use enumeration, additional return parameter or exception depending on situation.

introduced bool and '0' code confusion:

// sorry for abuse of "C" for educational purposes
bool isSame(s1,s2) { return s1==s2; }
int isSame2(s1, s2) { return strcmp(s1.cstr(), s2.cstr()); } 

if (isSame(..) && !isSame2(..)) ...
查看更多
等我变得足够好
3楼-- · 2019-07-15 16:59

In C++ it is common to use boolean return to indicate true for success, and false for failure.

It is also common to use an integer return value where zero indicates success, and non-zero to indicate failure or any other condition (often negative numbers can indicate errors, while positive indicate some other outcome).

Whichever approach you choose, the important thing is to document it and be consistent throughout the code base.

查看更多
\"骚年 ilove
4楼-- · 2019-07-15 17:10

Standard C functions that return an integer usually return greater-than or equal to zero for success and -1 for failure. When -1 is returned, errno is usually set to provide a more specific error. Zero indicates a generic success where numbers greater than zero are assumed to be successful and provide additional detail.

Consider something like recvfrom(). A return value of -1 indicates outright failure with errno set to a detailed code. A return value of zero means that the socket was shutdown in an orderly fashion and that additional calls will not return data. A return value greater than zero means that data was received and the number of bytes received is what was returned.

查看更多
Melony?
5楼-- · 2019-07-15 17:15

I understand you to be asking two questions:

  • how do you return program status in c++
  • how do functions return status in c++

Basic answers follow.

Typically the program (main()) returns 0 for success, and other values - errors - for failure

If a function can either succeed or fail, it is, typically, typed as bool and returns true or false.

If a function can return multiple values it will, typically, return int or enum values.

Try throwing exceptions for those cases which are exceptional.

查看更多
贪生不怕死
6楼-- · 2019-07-15 17:18

A C++ function or method can return a bool value like true or false. A program (i.e. the main function) must return an int value, for example 0 to report 'success'.

查看更多
登录 后发表回答