- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以创建一个扩展以下代码的宏
int error = 0;
struct parse_tree res = CLEANUP_parse(file_hdl);
// ...
cleanup:
return error
在语义上等同于(足够)
int error = 0;
struct parse_tree res;
struct parse_tree tmp = parse(file_hdl);
if(global_error_variable != 0) {
error = global_error_variable;
goto cleanup;
} else {
res = tmp;
}
// ...
cleanup:
return error
我担心它不可能的原因是 goto label;
是一个语句,我真的需要一个表达式。例如。像
#define CLEANUP_parse(file_hdl) ((tmp=parse(file_hdl)) ^ tmp ^ global_error_variable) ? goto cleanup : tmp
因为这个原因不起作用(并且因为 tmp
没有声明,可能更多)。
遗憾的是,编译器扩展只有在 GCC、VSC 和 Texas Instrument 的 C 编译器支持它们时才有可能。
我们有一个现有的代码库,其中每个函数返回一个整数错误代码,调用者跳转到 cleanup:
非零返回代码。我考虑引入使用全局错误代码来启用其返回值的函数,但希望以安全和向后兼容的方式进行。 The full picture on SE Code Review
最佳答案
Can I create a macro which conditionally
goto
s and serves as an expression?
不是一个表达式。考虑一种不同的方法,它只需要轻微的重构,但将与标准 C 兼容:
int error = 0;
struct parse_tree res;
CLEANUP_PARSE(res, file_hdl, error, cleanup);
// ...
cleanup:
return error;
那么你会:
#define CLEANUP_PARSE(res, file_hdl, error, cleanup) \
do { \
res = parse(file_hdl); \
if (global_error_variable != 0) { \
error = global_error_variable; \
goto cleanup; \
} \
} while(0)
当 GNU 扩展没问题时,您还可以添加带有语句表达式的 GNU 版本:
#ifdef __GNUC__
// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs
#define CLEANUP_PARSE_RET(file_hdl, ...) \
__extension__({ \
struct parse_tree _res; \
/* macro from above */ \
CLEANUP_PARSE(_res, file_hdl, __VA_ARGS__); \
_res; \
})
#endif
可以按您的预期使用:
struct parse_tree res = CLEANUP_PARSE_RET(file_hdl, error, cleanup);
并且库的用户可以选择他想要的版本,这取决于用户打算支持的编译器。
关于我可以创建一个有条件的 `goto` s 并用作表达式的宏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745172/
作为入门编程类(class)的家庭作业,我必须设计并实现一个生成随机数 (1-100) 的程序,然后为玩家提供 7 次猜测以正确猜出该数字。我想出了这个算法: 但是,我不知道如何将算法的这种概念表示转
这个问题在这里已经有了答案: 关闭 14 年前。 此问题与Goto Still Considered Harmful 重复.如果您想进一步讨论这个问题,请使用原始问题。 为什么 GOTO 是糟糕的编
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭3年前。 Improve th
我想知道在这种情况下您不应该使用 goto 而不是使用 while(variable) 是否有原因: :start result=askUser("do you want 1,2,or 3?") sw
是的,我知道 Esdger Dijkstra 说你不应该使用 goto ,但他不是上帝。我认为只要不过分使用无条件分支就可以了。过度使用继承和过度使用分支语句一样有可能创建不可读的代码。 无论如何,现
在 Visual Studio 中 Go To Definition 和 Go To Implementation 有什么区别? 版本:Visual Studio 2015 Update 1 最佳答案
Section 6.8.1 of C11或 C99 ,或 C89 的第 3.6.1 节所有这些似乎都表明 default 和 case x(其中 x 是一些 constant-expression)是
所以我下载了 sublime text 3,据称“Sublime Text 现在扫描项目中的文件,并构建哪些文件包含哪些符号的索引。这支持项目中的 Goto Definition 和 Goto Sym
当goto语句下面放置标签时,编译器如何找到标签的地址进行跳转? C 编程是自上而下的方法,对吗?如果标号位于 goto 语句之上,则可以理解,当标号到达 goto 语句时,其地址就已知。请大家解释一
谁能找出 VBA 中“On error goto -1”和“on error goto 0”之间的区别吗?我尝试过 google 和 msdn,但没有成功。 最佳答案 On Error GoTo 0
每当我运行 code下面它发生在我身上,我在使用 if 存在行时犯了一个错误,因为无论目录是否存在,它都表现为该行从未存在过...... echo off echo echo (c) Ryan L
我知道去函数声明的方法(使用 ctags + Ctrl + ])。但是,如果我不想转至函数声明,而是转至函数定义,该怎么办。 例如: // foo.c #include "foo.h" ... if
我正在尝试用防御性编程来完成这个小程序,但我很难处理这个避免 Loop-Goto 的问题,因为我知道这是糟糕的编程。我尝试过 while 和 do...while 循环,但在一种情况下我没有问题。当我
我正在尝试创建一个宏,允许用户单击按钮,Excel 工作表将执行 1:移至下一个/上一个工作表或 2:复制现有工作表,然后移至复制的工作表。 目前我陷入了处理错误处理情况的困境,并且不知道如何解决它。
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭5 个月前。 Improv
当他们谈论更喜欢汇编而不是更高级的语言时,我正在和某人通电话。他们提到喜欢 goto 语句,我回答说它会导致意大利面条代码。他们回复了“goto $”之类的内容。我说 goto 会散发出代码气味。他们
我有一个简单的数字根计算(即将整数中的所有数字相加,如果总数超过一位数字,则重复该过程,直到最终得到一位数字的答案)。 我的第一个冲动是简单地计算初始字符串中的数字之和,测试结果是否多于一位数字,如果
我对 C# 还很陌生(一周前开始学习),在批处理和表达式 2 方面有一些经验,并且我一直在开发基于文本的游戏,试图了解更多信息。我一开始使用了 goto 语句,但根据我发现的几乎每个人的说法,goto
#include int main() { int i; goto l; for(i = 0; i < 5; i++) { l:printf("Hi\n");
我必须编写一个 C 程序(不使用递归、else、for、while、do {} while 并且使用 if 的构造不得包含“return”)来从数组中查找最大数字。所以现在这是我的代码: #inclu
我是一名优秀的程序员,十分优秀!