- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道#undef 在 C 中的实际用途。我正在通过 K&R 工作,并且正在处理预处理器。其中大部分是我(或多或少)理解的 Material ,但第 90 页(第二版)上的某些内容让我印象深刻:
Names may be undefined with
#undef
, usually to ensure that a routine is really a function, not a macro:
#undef getchar
int getchar(void) { ... }
这是防御某人 #define
与您的函数同名的宏的常见做法吗?或者这真的更多的是现实中不会出现的样本? (EG,没有人在他的正确、错误或疯狂的头脑中应该重写 getchar()
,所以它不应该出现。)有了你自己的函数名称,你觉得有必要这样做吗?如果您正在开发供其他人使用的库,这会改变吗?
最佳答案
如果您阅读 Plauger 的 The Standard C Library (1992),你会看到 <stdio.h>
header 允许提供 getchar()
和 getc()
作为类似函数的宏(具有 getc()
的特殊权限,可以多次评估其文件指针参数!)。但是,即使它提供了宏,实现也有义务提供执行相同工作的实际函数,主要是为了您可以访问一个名为 getchar()
的函数指针。或 getc()
并将其传递给其他函数。
也就是说,通过做:
#include <stdio.h>
#undef getchar
extern int some_function(int (*)(void));
int core_function(void)
{
int c = some_function(getchar);
return(c);
}
正如所写,core_function()
毫无意义,但它说明了这一点。你可以用 isxxxx()
做同样的事情<ctype.h>
中的宏也是,例如。
通常,您不想这样做 - 您通常不想删除宏定义。但是,当你需要真正的功能时,你可以得到它。提供库的人可以很好地模拟标准C库的功能。
另请注意,您很少需要使用显式 #undef
的原因之一是因为您可以通过编写调用函数而不是宏:
int c = (getchar)();
因为getchar
之后的token不是 (
, 它不是类函数宏的调用,所以它必须是函数的引用。同样,上面的第一个示例即使没有 #undef
也能正确编译和运行。 .
如果您使用宏覆盖实现您自己的函数,您可以使用它来获得良好的效果,尽管除非解释,否则它可能会有点困惑。
/* function.h */
…
extern int function(int c);
extern int other_function(int c, FILE *fp);
#define function(c) other_function(c, stdout);
…
/* function.c */
…
/* Provide function despite macro override */
int (function)(int c)
{
return function(c, stdout);
}
函数定义行不调用宏,因为 function
之后的标记不是 (
. return
行确实调用了宏。
关于c - #undef-ing 在实践中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/216865/
几年前,当我最初学习 Perl 时,我发现自己想说一些类似的话: sleep .07; 但这实际上行不通。 有人教我使用: select undef, undef, undef, .07; 反而。 我
undef $x is different from $x = undef .我的印象是两者都会触发垃圾收集和释放内存,但似乎 $x = undef 不会这样做。 这是语言错误吗?在执行 $x = u
我正在使用铁路 api 构建 Web 应用程序,在提交火车号时我试图显示数据但出现上述错误。实际上我在获取数据时使用了 if else 条件。 下面是代码。 import React, { Com
我在 Perl 中有以下(从错误中理想化的)简短脚本: my %metadata = undef; if (defined %metadata) { print "defined"; } 出于
Julia 中检查数组条目是否为 #undef 的最佳方法是什么? 例子: julia> a = Array(Vector,2) julia> isdefined(a[1]) # fails jul
我有一个全局扩展,它定义了一个名为“is”的方法。我遇到的问题是 ESLink 将其标记为“no-undef”;尽管它在运行时可用。 禁用规则对我来说不是一个选择。我宁愿定义它以便 ESLint 可以
在 Perl 中读取配置文件时,可能会出现某行无效且不需要将其添加到我的有效行数组中的情况。由于我在这里使用 for 循环,因此即使无效行也会创建一个 undef 条目。之后我怎样才能将它们全部删除?
有时候我想写: my $var = shift // undef; # argument is optional # or my $va
我定义了一堆宏,它们只在项目的一小部分范围内发挥作用。我想取消定义它们,这样它们就不会污染全局命名空间,但 Visual Studio 仍然会在其他文件中的#undef 之后确认它们的存在。即: //
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: undef - Why would you want to undefine a method in rub
我正在将一些参数传递给函数,其中一个可能未定义。 $a = ($config->function(param('a'),'int')); 我的模块包含一个如下所示的函数: sub function{
我在一个数组中定义了一些元素: my @arrTest; $arrTest[0]=1; $arrTest[2]=2; #Result for @arrTest is: (1, undef, 2) if
有什么办法可以从 no-undef 中排除特定模式吗?错误。我试过 'no-undef': [2, { ignoreBuiltinGlobals: ['app'] }]哪里app是由 webpack.
我使用教程来学习 React,但很快就卡住了。在教程中,他们使用了这段代码: import React, { Component } from "react"; class Counter exten
我有一个 Moose 类,它的属性起到一定的作用: has foo => (is => 'rw', does => 'Foo::Bar'); 现在我如何更改声明以允许 undef ?我试过 does
我今天似乎很傻,或者我有一个很奇怪的问题。请考虑以下代码: #!/usr/bin/perl use strict; use warnings; use warnings::unused; use wa
我是 react 新手。我的应用程序运行正常,但添加 eslint(airbnb) 后,我的应用程序无法编译。 export default class HelloWorldComponent ext
尝试编译我的应用程序时出现以下错误 - 'getRestaurants' 未定义 no-undef 我无法查明为什么没有为 zipCodeToCoords 定义 getRestaurants 我有一个
我制作了cardList,想通过 map 将卡片添加到cardlist中,但出现此错误 import React from 'react'; import Card from './Card'; co
我想在表的列中插入 null。 哪一个代表 null? undef 或空字符串 ''。 应该使用哪一个,为什么?我知道已定义并且我可以检查它。 但我更多的是从数据库的角度来看。 哪一个更恰本地表示nu
我是一名优秀的程序员,十分优秀!