assert.h 中的 C++ 断言实现 [英] C++ assert implementation in assert.h

查看:39
本文介绍了assert.h 中的 C++ 断言实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

The question is: what is for the "(void)" on line 34 and what is __assert?

推荐答案

看这一行:

extern void __assert (const char *msg, const char *file, int line);

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

__assert is function that takes an assertion message, a file name and a line number as arguments. Basically, this is the method that prints out the error message and terminates the program when the assertion failed.

再看上面的宏定义:

#define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0))

它定义了 assert(EX) 宏,所以它首先检查 EX 表达式并且(因为 C++ || 运算符的短路操作)只如果失败,它会调用 __assert 函数并将失败的断言异常作为字符串传递,以及源文件中 assert() 方法调用的确切位置.使用这种预处理器技巧,断言库可以在您在程序中键入以下内容时实现这一点

It defines the assert(EX) macro so, it first checks the EX expression and (because of the short-circuit operation of the C++ || operator) only if it fails, it calls the __assert function and passes the failed assertion exception as a string, and the exact location of the assert() method call in your source files. With this preprocessor trickery the assertion library achieves that when you type the following in your program

assert(a == 0);

并且你的断言在程序运行过程中失败,你会得到详细的

and your assertion fails during the program run, you get the detailed

Assertion failed: a == 0 at program.c, line 23

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

error message that helps you to identify the exact location where the assertion was failing in your code.

(void) 部分只是为了确保编译器不会对 (EX) || 的未使用结果发出一些警告0 表达,看其他答案,大佬解释的很好.

The (void) part is just for make sure that the compiler won't put up some warnings about the unused result of the (EX) || 0 expression, see the other answers, the guys explained it well.

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

The remaining preprocessor define NDEBUG is used to turn of assertion generation at all compile-time, you your resulting executable will be smaller and faster.

这篇关于assert.h 中的 C++ 断言实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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