如何针对每个宏值执行不同的操作?

发布于 2024-12-07 06:43:09 字数 115 浏览 0 评论 0原文

#define TYPE char *

if TYPE is char *
  do A
if TYPE is int
  do B

有没有例子如何做这样的事情?

#define TYPE char *

if TYPE is char *
  do A
if TYPE is int
  do B

Is there an example how to do such things?

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

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

发布评论

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

评论(5

雨夜星沙 2024-12-14 06:43:09

C 预处理器 MACROS 操作文本,因此本质上是无类型的,所以不,你不能这样做。

C preprocessor MACROS manipulate text, so are essentially typeless, so NO you can't do that.

忘羡 2024-12-14 06:43:09

您可以将另一个符号与其关联:

#define TYPE char *
#define TYPE_IS_CHAR_STAR

#ifdef TYPE_IS_CHAR_STAR
...
#endif

您只需手动保持它们一致即可。

请注意,这是一个危险的宏;您应该使用 typedef 来代替。对于宏:

TYPE x, y;

x 是一个指针,但 y 不是。

You could associate another symbol with it:

#define TYPE char *
#define TYPE_IS_CHAR_STAR

#ifdef TYPE_IS_CHAR_STAR
...
#endif

You just need to keep them consistent manually.

Note that that's a dangerous macro; you should use a typedef instead. With the macro:

TYPE x, y;

x is a pointer, but y isn't.

挥剑断情 2024-12-14 06:43:09

您可以通过定义另一个宏和类型并使用 #ifdef 等来获得类似的效果。例如:

#define TYPE char *
#define TYPE_IS_PCHAR 1

...然后稍后...

#ifdef TYPE_IS_PCHAR
   do A
#endif
#ifdef TYPE_IS_INT
   do B
#endif

这不完全相同,但它仍然可以让您到达那里。

You can get a similar effect by defining another macro along with the type, and using #ifdef etc. with that other macro. For example:

#define TYPE char *
#define TYPE_IS_PCHAR 1

...then later...

#ifdef TYPE_IS_PCHAR
   do A
#endif
#ifdef TYPE_IS_INT
   do B
#endif

It's not quite the same thing, but it still gets you there.

水溶 2024-12-14 06:43:09

不容易。你可以这样做:

#define TYPE_IS_CHARPTR
//#define TYPE_IS_INT

#ifdef TYPE_IS_CHARPTR
    do A
#endif
#ifdef TYPE_IS_INT
    do B
#endif

但是你真的应该尽量减少预处理器对棘手事情的使用(除了简单变量之外的任何事情)。

有了枚举常量和内联函数,现在就不再需要这样的用途了。

Not easily. You could do something like:

#define TYPE_IS_CHARPTR
//#define TYPE_IS_INT

#ifdef TYPE_IS_CHARPTR
    do A
#endif
#ifdef TYPE_IS_INT
    do B
#endif

But you really should be trying to minimise your use of the preprocessor for tricky things (anything other than simple variables).

With enumerated constants and inline functions, there's little need for such uses nowadays.

╄→承喏 2024-12-14 06:43:09

如果您只使用基本类型(因为它们只是字符串 - 请参阅米奇的答案),它会起作用。但是一旦您尝试使用指针,它就不再起作用 - 星号会引发预处理器循环:

[holt@Michaela ~]$ gcc test.c
test.c:3:10: error: operator '*' has no right operand

但是如果您想根据不同类型做不同的事情,我将不得不建议切换到C++ 并使用 模板模板专业化。很不情愿,因为模板语法非常丑陋,但你应该能够做任何你想做的事情。

希望有帮助!

It would work if you just used basic types (since they're just strings - see Mitch's answer). But as soon as you try to use pointers, it won't work any more - the asterisk throws the preprocessor for a loop:

[holt@Michaela ~]$ gcc test.c
test.c:3:10: error: operator '*' has no right operand

But if you want do do different things based on different types, I'm going to have to recommend switching to C++ and using templates and template specialization. Reluctantly, since template syntax is incredibly ugly, but you should be able to do whatever you want.

Hope that helps!

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