- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在什么情况下我们应该包含cassert?
最佳答案
简而言之,不要使用它;使用 <assert.h>
.
C++11 删除了“c....” header 不会污染全局命名空间的任何形式保证。
这从来都不是实际保证,现在甚至不是正式保证。
因此,对于 C++11,使用“c....” header 变体不再有任何可以想象的优势,而与一个编译器及其版本一起工作的代码存在明显而明显的缺点编译器,可能无法使用其他编译器或版本编译,例如全局命名空间中的名称冲突或不同的重载选择。
所以,而 cassert
在 C++03 中是毫无意义的(你不能将宏放在命名空间中),在 C++11 中它完全没有意义——即使作为一般方案的特例。
附录,2013 年 12 月 22 日:
标准根据
C++11 §D.5/2:
“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(非规范示例):
“The header
<cstdlib>
assuredly provides its declarations and definitions within the namespacestd
. 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 namespacestd
.”
堆栈溢出用户 C.R.的评论让我意识到某些版本的 g++,例如 MinGW g++ 4.7.2,相对于 <X.h>
是非常非标准。 header ,缺少例如的重载sin
C++ 标准要求:
我已经知道MinGW g++ 4.7.2也完全没有swprintf
等功能,并且它在纯 C++ 库中也有同样的缺点,例如缺少 C++11 std::to_string
.然而,关于它缺少 C 函数重载的信息对我来说是新的。
在实践中,g++ 缺少重载意味着
忽略 g++ 问题,或
避免使用缺少的 g++ 重载,
例如仅使用 double sin( double )
, 或
使用 std
命名空间重载
(然后需要包含 <cmath>
以保证它们在 g++ 中的存在)。
为了使用 g++ std
命名空间重载不合格,一种实用的方法是为此编译器定义 headers wrappers。我已经使用这种方法来解决 g++ 的缺点。到printf
家庭。正如 David Wheeler 曾经说过的那样,“计算机科学中的所有问题都可以通过另一个层次的间接性来解决”……
然后可以安排事情,以便使用 g++ 缺失重载的标准代码也可以使用 g++ 编译。这会将编译器调整为标准,并使用固定数量的代码。
关于c++ - 在什么情况下我们需要包含<cassert>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10625716/
我是 Java 新手,这是我的代码, if( a.name == b.name && a.displayname == b.displayname && a.linknam
在下面的场景中,我有一个 bool 值。根据结果,我调用完全相同的函数,唯一的区别是参数的数量。 var myBoolean = ... if (myBoolean) { retrieve
我是一名研究 C++ 的 C 开发人员: 我是否正确理解如果我抛出异常然后堆栈将展开直到找到第一个异常处理程序?是否可以在不展开的情况下在任何 throw 上打开调试器(即不离开声明它的范围或任何更高
在修复庞大代码库中的错误时,我观察到一个奇怪的情况,其中引用的动态类型从原始 Derived 类型更改为 Base 类型!我提供了最少的代码来解释问题: struct Base { // some
我正在尝试用 C# 扩展给定的代码,但由于缺乏编程经验,我有点陷入困境。 使用 Visual Studio 社区,我尝试通过控制台读出 CPU 核心温度。该代码使用开关/外壳来查找传感器的特定名称(即
这可能是一个哲学问题。 假设您正在向页面发出 AJAX 请求(这是使用 Prototype): new Ajax.Request('target.asp', { method:"post", pa
我有以下 HTML 代码,我无法在所有浏览器中正常工作: 我试图在移动到
我对 Swift 很陌生。我如何从 addPin 函数中检索注释并能够在我的 addLocation 操作 (buttonPressed) 中使用它。我正在尝试使用压力触摸在 map 上添加图钉,在两
我设置了一个详细 View ,我是否有几个 Nib 文件根据在 Root View Controller 的表中选择的项目来加载。 我发现,对于 Nibs 的类,永远不会调用 viewDidUnloa
我需要动态访问 json 文件并使用以下代码。在本例中,“bpicsel”和“temp”是变量。最终结果类似于“data[0].extit1” var title="data["+bpicsel+"]
我需要使用第三方 WCF 服务。我已经在我的证书存储中配置了所需的证书,但是在调用 WCF 服务时出现以下异常。 向 https://XXXX.com/AHSharedServices/Custome
在几个 SO 答案(1、2)中,建议如果存在冲突则不应触发 INSERT 触发器,ON CONFLICT DO NOTHING 在触发语句中。也许我理解错了,但在我的实验中似乎并非如此。 这是我的 S
如果进行修改,则会给出org.hibernate.NonUniqueObjectException。在我的 BidderBO 类(class)中 @Override @Transactional(pr
我使用 indexOf() 方法来精细地查找数组中的对象。 直到此刻我查了一些资料,发现代码应该无法正常工作。 我在reducer中尝试了上面的代码,它成功了 let tmp = state.find
假设我有以下表格: CREATE TABLE Game ( GameID INT UNSIGNED NOT NULL, GameType TINYINT UNSIGNED NOT NU
代码: Alamofire.request(URL(string: imageUrl)!).downloadProgress(closure: { (progress) in
我是一名优秀的程序员,十分优秀!