C++断言assert.h中的实现

Nar*_*rek 12 c++ implementation assert

00001 /* assert.h
00002    Copyright (C) 2001, 2003 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00028 #ifndef _ASSERT_H
00029 #define _ASSERT_H
00030 
00031 #ifdef NDEBUG
00032 # define assert(EX)
00033 #else
00034 # define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0))
00035 #endif
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00041 extern void __assert (const char *msg, const char *file, int line);
00042 
00043 #ifdef __cplusplus
00044 };
00045 #endif
00046 #endif
Run Code Online (Sandbox Code Playgroud)

问题是:第34行的"(void)"是什么,__ ask是什么?

buc*_*buc 14

看看这行:

extern void __assert (const char *msg, const char *file, int line);
Run Code Online (Sandbox Code Playgroud)

__assert是一个函数,它将断言消息,文件名和行号作为参数.基本上,这是打印出错误消息并在断言失败时终止程序的方法.

然后看看上面的宏定义:

#define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0))
Run Code Online (Sandbox Code Playgroud)

它定义了assert(EX)宏这样,它首先检查EX表达和(因为C++的短路操作的||操作者),只有当它出现故障时,它调用__assert函数,并传递失败的断言例外作为一个字符串,和的确切位置assert()源文件中的方法调用.使用此预处理器技巧,断言库可以在您的程序中键入以下内容时实现

assert(a == 0);
Run Code Online (Sandbox Code Playgroud)

并且您的断言在程序运行期间失败,您将获得详细信息

Assertion failed: a == 0 at program.c, line 23
Run Code Online (Sandbox Code Playgroud)

错误消息,可帮助您确定代码中断言失败的确切位置.

(void)部分只是为了确保编译器不会对(EX) || 0表达式的未使用结果发出一些警告,请参阅其他答案,这些人解释得很清楚.

剩余的预处理器定义NDEBUG用于在所有编译时转换断言生成,您生成的可执行文件将更小更快.

  • 1)[逗号运算符](http://en.wikipedia.org/wiki/Comma_o​​perator)(`,`)首先执行左边的表达式(`__assert`调用)然后执行右边的表达式(字面值为0) `)并返回后者的结果.这是必需的,因为`||`运算符需要一个布尔兼容的操作数,但`__sert`函数返回`void`. (4认同)
  • 2)想象一个陈述`(a == 3)|| (0);`独自一人.执行比较然后对结果不做任何事情都没有多大意义.编译器也会识别出这一点并怀疑您忘记完成该声明并向您发出警告.通过将整个表达式转换为`void`:`(void)((a == 3)||(0))`你告诉编译器这个表达式没有合理的结果值(比如调用`void`-返回功能也不会),所以它不会抱怨它. (4认同)

Jam*_*nze 7

__assert是实施的一部分; 在这种情况下,库中的函数将在断言失败时被调用.而且(void)只是关闭编译器有关||运算符未使用结果的警告.