可选的初始化程序,通过预处理器技巧?

发布于 2024-08-06 09:26:56 字数 375 浏览 1 评论 0原文

我知道这行不通,但希望你能看到我正在尝试做的事情

#if ASSIGN_ALLOWED
    #define MAYBE_SKIP_REST_OF_LINE 
#else
    #define MAYBE_SKIP_REST_OF_LINE ; //
#endif

char str[80]  MAYBE_SKIP_REST_OF_LINE = "Hello\n";
long array[3] MAYBE_SKIP_REST_OF_LINE = { 7,8,9 };
int x         MAYBE_SKIP_REST_OF_LINE = 3;
//...many many more similar lines...

有没有办法做到这一点,使其有效?

I know that this will not work, but hopefully you can see what I'm trying to do

#if ASSIGN_ALLOWED
    #define MAYBE_SKIP_REST_OF_LINE 
#else
    #define MAYBE_SKIP_REST_OF_LINE ; //
#endif

char str[80]  MAYBE_SKIP_REST_OF_LINE = "Hello\n";
long array[3] MAYBE_SKIP_REST_OF_LINE = { 7,8,9 };
int x         MAYBE_SKIP_REST_OF_LINE = 3;
//...many many more similar lines...

Is there a way to do this such that it works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飞烟轻若梦 2024-08-13 09:26:56

当然:

#ifdef ASSIGN_ALLOWED
    #define OPTIONAL_INITIALISER(x) = x 
#else
    #define OPTIONAL_INITIALISER(x) 
#endif

char str[80] OPTIONAL_INTIALISER("Hello\n");
#define ARRAY_INIT { 7,8,9 }
long array[3] OPTIONAL_INITIALISER(ARRAY_INIT);
#undef ARRAY_INIT
int x OPTIONAL_INITIALISER(3);

任何包含逗号的初始化程序(例如示例中的 array)都需要从其自己的宏进行扩展,例如上面的 ARRAY_INIT 。如果您的编译器支持 C99 可变参数宏,那么您可以以更简洁的方式进行操作:

#ifdef ASSIGN_ALLOWED
    #define OPTIONAL_INITIALISER(...) = __VA_ARGS__ 
#else
    #define OPTIONAL_INITIALISER(...) 
#endif

char str[80] OPTIONAL_INTIALISER("Hello\n");
long array[3] OPTIONAL_INITIALISER({ 7,8,9 });
int x OPTIONAL_INITIALISER(3);

Sure:

#ifdef ASSIGN_ALLOWED
    #define OPTIONAL_INITIALISER(x) = x 
#else
    #define OPTIONAL_INITIALISER(x) 
#endif

char str[80] OPTIONAL_INTIALISER("Hello\n");
#define ARRAY_INIT { 7,8,9 }
long array[3] OPTIONAL_INITIALISER(ARRAY_INIT);
#undef ARRAY_INIT
int x OPTIONAL_INITIALISER(3);

Any initialisers containing commas, like for array in the example, will need to be expanded from their own macro, like ARRAY_INIT in the above. If your compiler supports C99 varargs macros, then you can instead do it in a cleaner way:

#ifdef ASSIGN_ALLOWED
    #define OPTIONAL_INITIALISER(...) = __VA_ARGS__ 
#else
    #define OPTIONAL_INITIALISER(...) 
#endif

char str[80] OPTIONAL_INTIALISER("Hello\n");
long array[3] OPTIONAL_INITIALISER({ 7,8,9 });
int x OPTIONAL_INITIALISER(3);
独自唱情﹋歌 2024-08-13 09:26:56

由于注释在预处理器运行中被过滤掉,我不这么认为

As comments are filtered out in the preprocessor run, i dont think so

走过海棠暮 2024-08-13 09:26:56

这取决于预处理器如何处理注释和宏。如果它在宏扩展后删除注释,那么您的航行就会一帆风顺,但否则它可能仅仅由于预处理器的实现而无法工作。

你可以试试这个吗? (虽然会很乱)。

#define MAYBE_SKIP(code) code
#define MAYBE_SKIP(code) /* code */

It would depend on how the preprocessor worked with comments and macros. If it strips comments after macro expansion then your smooth sailing, but otherwise it might not work simply due to the preprocessor implementation.

You could try this? (it would be messy though).

#define MAYBE_SKIP(code) code
#define MAYBE_SKIP(code) /* code */
薆情海 2024-08-13 09:26:56

预处理器会删除注释部分。尝试运行

gcc -E source.c

这将在您的代码上运行预处理器,但不会实际编译它,从而允许您查看宏扩展后会发生什么。您应该注意到,所有扩展宏中的所有注释都消失了。

The preprocessor strips out commented sections. Try running

gcc -E source.c

This will run the preprocessor on your code but will not actually compile it, allowing you to see what happens after macro expansion. You should notice that all the comments are gone from any expanded macros.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文