在什么情况下我们需要包含 <cassert>? [英] In what cases we need to include <cassert>?

查看:20
本文介绍了在什么情况下我们需要包含 <cassert>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下我们应该包含cassert?

In what cases should we include cassert?

推荐答案

总之,不要用;使用 .

In short, don't use it; use <assert.h>.

C++11 删除了c...."标头不会污染全局命名空间的任何正式保证.

C++11 removed any formal guarantee of a "c...." header not polluting the global namespace.

这从来都不是实际的保证,现在甚至都不是正式的保证.

It was never an in-practice guarantee, and now it's not even a formal guarantee.

因此,在 C++11 中,使用c...."头文件变体不再有任何可以想象的优势,而有一个明显的缺点,即代码在一个编译器和它的版本上运行良好编译器,可能无法使用另一个编译器或版本进行编译,例如全局命名空间中的名称冲突或不同的重载选择.

Hence, with C++11 there is no longer any conceivable advantage in using the "c...." header variants, while there is the distinct and clear disadvantage that code that works well with one compiler and version of that compiler, may fail to compile with another compiler or version, due to e.g. name collisions or different overload selection in the global namespace.

SO,虽然 cassert 在 C++03 中毫无意义(你不能将宏放在命名空间中),但它完全没有意义——即使作为通用方案的特例-- 在 C++11 中.

SO, while cassert was pretty meaningless in C++03 (you can't put a macro in a namespace), it is totally meaningless -- even as a special case of a general scheme -- in C++11.

附录,2013 年 12 月 22 日:

Addendum, Dec 22 2013:

标准定义了每个 C++ C 头文件 就 <cX> 而言的标头头文件,而后者又是根据相应的 C 库头文件定义的.

The standard defines each C++ C header <X.h> header in terms of the <cX> header, which in turn is defined in terms of the corresponding C library header.

C++11 §D.5/2:

“每个 C 头文件,每个头文件都有一个 name.h 形式的名称,就像每个名称都被相应的 cname 标头放置在全局命名空间范围内.”

“Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope.”

C++11 §D.5/3(非规范性示例):

“头文件 确实在名称空间 std 内提供了它的声明和定义.它还可以在全局命名空间内提供这些名称.头文件 确实在全局命名空间中提供了相同的声明和定义,就像在 C 标准中一样.它还可以在名称空间 std 中提供这些名称.”

“The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std.”

Stack Overflow 用户 CR 的评论让我意识到某些版本的 g++,例如 MinGW g++ 4.7.2,相对于 标头来说是相当非标准的,缺少 eg 的重载C++标准要求的sin:

Stack Overflow user C.R.’s comment made me aware that some versions of g++, such as MinGW g++ 4.7.2, are quite non-standard with respect to the <X.h> headers, lacking the overloads of e.g. sin that the C++ standard requires:

我已经知道 MinGW g++ 4.7.2 也完全没有 swprintf 等功能,而且它在纯 C++ 库中也有同样的缺点,例如缺乏 C++11 std::to_string.然而,关于它缺少 C 函数重载的信息对我来说是新的.

I already knew that MinGW g++ 4.7.2 also entirely lacks functions such as swprintf, and that it has ditto shortcomings in the pure C++ library such as lacking C++11 std::to_string. However, the information about it lacking the C function overloads was new to me.

在实践中缺少 g++ 的重载意味着

In practice the lacking overloads with g++ means

  • 忽略 g++ 问题,或

  • ignoring the g++ issue, or

避免使用缺少的 g++ 重载,
例如仅使用 double sin( double ),或

avoiding using the missing g++ overloads,
e.g. using only double sin( double ), or

使用 std 命名空间重载
(然后需要包含 以保证它们与 g++ 的存在).

using the std namespace overloads
(one then needs to include <cmath> to guarantee their presence with g++).

为了使用不合格的 g++ std 命名空间重载,一种实用的方法是为此编译器定义 headers 包装器.我已经使用这种方法来解决 g++ 的缺点.printf 家族.正如大卫·惠勒 (David Wheeler) 曾经说过的那样,计算机科学中的所有问题都可以通过另一个间接层次来解决"…

In order to use the g++ std namespace overloads unqualified, one practical approach is to define headers wrappers for this compiler. I've used that approach to address g++ shortcomings wrt. to the printf family. For as David Wheeler once remarked, “All problems in computer science can be solved by another level of indirection”…

然后可以安排一些事情,以便使用 g++ 缺少的重载的标准代码也可以使用 g++ 进行编译.这会将编译器调整为标准,代码量固定.

Then things can be arranged so that standard code that uses g++'s missing overloads, also compiles with g++. This adjusts the compiler to the standard, with a fixed amount of code.

这篇关于在什么情况下我们需要包含 &lt;cassert&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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