为什么返回负的 errno?(例如返回 -EIO) [英] Why return a negative errno? (e.g. return -EIO)
问题描述
另一个简单的例子:
if (wpa_s->mlme.ssid_len == 0)
return -EINVAL;
为什么是一元减号?这是(通常)为成功时返回 >0 和失败时返回 <(=)0 的函数完成的,还是有其他原因?
Why the unary minus? Is this (usually) done for functions that return >0 on success and <(=)0 on failure, or is there some other reason?
推荐答案
基本上就是这个原因.许多函数都有许多好的"正结果,因此错误代码为负值.
That's basically the reasons. Lots of functions have lots of "good" positive results, so that leaves the negative values for error codes.
C/POSIX 错误代码有点历史性增长",因此试图将过多的押韵或原因归咎于它们没有多大意义.
C / POSIX error codes are a bit "historically grown," so there's not much sense in trying to attribute too much rhyme or reason to them.
更多现代语言会抛出错误异常,这样它们就不必劫持部分可能的错误代码响应范围.当然,这两种方式都需要权衡.
Many more modern languages throw exceptions for errors so that they don't have to hijack part of their possible response range for error codes. Of course there are trade-offs either way.
这篇关于为什么返回负的 errno?(例如返回 -EIO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!