C++ 错误代码 vs 断言 vs 异常选项选择:( [英] C++ error-codes vs ASSERTS vs Exceptions choices choices :(

查看:28
本文介绍了C++ 错误代码 vs 断言 vs 异常选项选择:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题的代码

我在围栏的两边都听过(并反刍)C++ 异常咒语.已经有一段时间了,我只想再次集中注意力,本次讨论特定于我链接的代码(或低级类,如容器),以及它的依赖关系.我曾经是一个使用 C 语言的防御和 error_code 程序员,但这是一个令人厌烦的实践,我现在在更高的抽象层次上编程.

I have heard (and regurgitated) the C++ exception mantra on both sides of the fence. It has been a while and I just want to centre myself once more, and this discussion is specific to the code I have linked (or low level classes such as containers), and it's dependencies. I used to be a defensive and error_code using C programmer, but it's a tiresome practise and I am programming at a higher level of abstraction now.

所以我正在重写一个容器类(及其依赖项),以使其更灵活且可读性更好(没有 atm 的迭代器).如您所见,我正在返回枚举的 error_codes,我知道我将在调用站点测试它们.容器用于 AST 的运行时构建、初始化和只读.例外是为了防止容器被天真地使用(将来可能由我自己).

So I am rewriting a container class (and it's dependencies) to be more flexible and read better (iterators absent atm). As you can see I am returning enumerated error_codes where I know I will test them at call-site. The containers are for runtime building of AST's, initialize and make read-only. The exceptions are their to prevent the container being used naively (possibly by myself in the future).

我在这门课上到处都是例外,它们让我觉得很脏.我很欣赏他们的用例.如果我有选择,我可能会完全关闭它们(Boost 经常使用异常,我正在构建 Boost,是的,我知道它们可以被禁用,但是在罗马时......).我可以选择用 error_codes 替换它们,但是嘿,我不会测试它们,那有什么意义呢?

I have exceptions all over the place in this class, and they make me feel dirty. I appreciate their use-case. If I had the choice I might turn them off altogether (Boost uses exceptions a lot, and I am building off Boost, and yes I know they can be disabled, but when in Rome....) . I have the choice of replacing them with error_codes but hey, I will not test them , so what is the point ?

我应该用 ASSERTS 替换它们吗?人们谈论的膨胀是什么[1] [2] [3]?每个函数调用站点都获得额外的机器吗?还是只有那些有 catch 子句的?既然我不会捕捉到这些异常,我不应该成为这种膨胀的受害者,对吗?在基本原始类(即容器)的上下文中,断言不会进入发布版本,这甚至重要吗?我的意思是逻辑错误进入最终构建的可能性有多大?

Should I replace them with ASSERTS ? What is this bloat people speak off [1] [2] [3]? does every function callsite get extra machinery ? or only those that have a catch clause ? Since I won't catch these exceptions I shouldn't be a victim of this bloatage right ? ASSERTS do not make their way into release builds, in the context of fundamental primitive classes ( -- i.e, containers) does that even matter ? I mean how high are the chances that logic errors would find their way into a final build ?

因为我们喜欢回答重点问题,这里是我的:你会做什么,为什么? :D

Since we like to answer focused questions, here is mine: What would you do, and why ? :D

无关链接:错误代码并让它们在异常中捎带.

edit 2 在这种特殊情况下,选择是在 ASSERT 和异常之间,我认为异常最有意义,正如我上面提到的,容器在初始化后才被读取,并且大多数异常在初始化期间触发.

edit 2 in this particular case the choice is between ASSERTs and exceptions, I think exceptions make the most sense, as I mentioned above, the container is read only after initialisation, and most of the exceptions are triggered during initialisation.

推荐答案

很简单.避免像火这样的错误代码并更喜欢异常,除非错误代码在个别情况下确实更有意义.为什么?因为异常可以携带更多信息——参见例如Boost.Exception.因为它们会自动传播,所以您不会犯检查错误条件的错误.因为有时,您必须(摆脱构造函数),所以为什么不保持一致.C++ 根本没有提供任何更好的方法来通知错误条件.

It's very simple. Avoid error codes like fire and prefer exceptions, unless error code really makes more sense in an individual case. Why? Because exceptions can carry a lot more information — see e.g. Boost.Exception. Because they propagate automatically, so you can't make a mistake of not checking for error condition. Because sometimes, you have to (bailing out of a constructor), so why not be consistent. C++ simply does not offer any better way for signalling error conditions.

另一方面,断言用于完全不同的事情——验证代码的内部状态,以及应该始终成立的假设.失败的断言总是一个错误——而异常可能表示无效的外部输入,例如.

Asserts, on the other hand, are used for something completely different — verifying internal state of the code, and assumptions that should always hold true. Failed assertion is always a bug — whereas exception might signal invalid external input, for example.

至于链接指南:忘记谷歌风格指南甚至存在,这简直太糟糕了,这不仅仅是我的意见.LLVM — 可执行文件的大小几乎无关紧要,这不是您真正应该浪费时间思考的事情.Qt — Qt 缺乏异常安全,但这并不意味着您的代码也必须如此.采用现代做法,异常安全应该不会太难.

As for linked guides: forget Google style guide even exists, it's simply terrible, and it's not just my opinion. LLVM — executable size hardly matters, it's not really something you should waste time thinking about. Qt — Qt is lacking in exception safety, but that doesn't mean your code has to, too. Use modern practices, and being exception safe shouldn't be too hard.

这篇关于C++ 错误代码 vs 断言 vs 异常选项选择:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆