简体   繁体   English

类似于函数的宏,与类似对象的宏同名

[英]function-like macro with the same name as an object-like macro

Is it possible to have in C something like: 有可能在C中有这样的东西:

#define MACRO_EX 333

#define MACRO_EX(X,Y) ((X) < (Y) ? : (X) : (Y))

Can they coexist? 他们可以共存吗?

The C standard says (ISO/IEC 9899:1999, §6.10.3, 2): C标准说(ISO / IEC 9899:1999,§6.10.3,2):

An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical. 除非第二个定义是类似于对象的宏定义且两个替换列表相同,否则当前定义为类似对象宏的标识符不应由另一个#define预处理指令重新定义。 Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define preprocessing directive unless the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical. 同样,当前定义为类似函数宏的标识符不应由另一个#define预处理指令重新定义,除非第二个定义是具有相同数量和参数拼写的类函数宏定义,并且两个替换列表相同。

So the answer is no. 所以答案是否定的。

No. A function-like (with args) and an object-like macro (no args) can not be defined with the same name (in standardese, attempting to do so violates a shall not rule, which means the compiler is required to diagnose it). 不能。类似函数(带有args)和类似对象的宏(无args)不能用相同的名称定义(在标准中,尝试这样做违反了不应该规则,这意味着编译器需要诊断它)。 However, C99 specifies variadic macros (taking 1 or more args). 但是,C99指定了可变参数宏(采用1个或多个args)。 Maybe that'll do what you want? 也许这会做你想要的?

You are free to #undef MACRO_EX and redefine it with args, if that solves your problem. 您可以自由#undef MACRO_EX并使用args重新定义它,如果这可以解决您的问题。 But it can be used only with or without args. 但它只能在有或没有args的情况下使用。

warning: "MACRO_EX" redefined [enabled by default]

In this program, 在这个节目中,

    #include <stdio.h>
    #include <stdlib.h>

    #define MACRO_EX 333
    #define MACRO_EX(X,Y) ((X) < (Y) ? : (X) : (Y))

    int main()
    {

    printf("\n %d  %d\n", MACRO_EX, MACRO_EX(10,20));
    printf("\n %d  %d\n", MACRO_EX);

    return 0;
    }

Im getting 我越来越

 warning: "MACRO_EX" redefined [enabled by default]
 note: this is the location of the previous definition
 In function ‘main’:
 error: ‘MACRO_EX’ undeclared (first use in this function)
 note: each undeclared identifier is reported only once for each function it appears in
 error: expected ‘)’ before ‘:’ token

If I comment one macro & it's usage then works fine :) 如果我评论一个宏&它的用法然后工作正常:)

But As per C99 Std 但按照C99标准

6.10.3 6.10.3

An identifier currently defined as an object-like macro shall not be redefined by another #define pre-processing directive unless the second definition is an object-like macro definition and the two replacement lists are identical. 除非第二个定义是类似于对象的宏定义且两个替换列表相同,否则当前定义为object-like宏的标识符不应由另一个#define预处理指令重新定义。 Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define pre-processing directive unless the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical. 同样,当前定义为类似function-like宏的标识符不应由另一个#define预处理指令重新定义,除非第二个定义是具有相同数字和参数拼写的类函数宏定义,以及两个替换列表是相同的。

I tried the same code by compiling with -std=c99 still getting same error. 我通过编译-std=c99尝试相同的代码仍然得到相同的错误。

see 看到

http://codepad.org/0nfvOGTl http://codepad.org/0nfvOGTl

#include<stdio.h>
#define MACRO_EX 333

#define MACRO_EX(X,Y) ((X) < (Y) ? : (X) : (Y))


int main()
{

printf("valud is %d",MACRO_EX);
return 0;
}

error 错误

Line 0: warning: "MACRO_EX" redefined
Line 0: warning: this is the location of the previous definition
In function 'main':
Line 10: error: 'MACRO_EX' undeclared (first use in this function)
Line 10: error: (Each undeclared identifier is reported only once
Line 10: error: for each function it appears in.)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM