这#define合法吗? [英] Is this #define legal?

查看:159
本文介绍了这#define合法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我很担心这样一个定义的合法性:


#define funcX函数


其中funcX属于* standard * C功能。这样做是否合法

?标准说标题中声明的任何函数可能是另外实现为标题中定义的宏,因此如果它的库不具有明确的声明包括标题。


这是否适用于标准功能?并且,如果定义

在定义funcY函数的标题之外,如果

我在源文件中使用它会怎么样?


谢谢!


-

Sensei< se ****** @ mac.com>


乐观主义者认为这是所有可能世界中最好的。

悲观主义者担心这是真的。 [J. Robert Oppenheimer]

解决方案

Sensei写道:


嗨!我很担心这样一个定义的合法性:


#define funcX函数


其中funcX属于* standard * C功能。这样做是否合法

?标准说标题中声明的任何函数可能是另外实现为标题中定义的宏,因此如果它的库不具有明确的声明标题包括在内。



如果相应的标题

已经#define''d funcX且你的funcY没有发生,那么程序可能无法编译br />
完全匹配现有定义。例如


#include< string.h>

#define strlen my_strlen


可能会失败,因为< string.hmay已经包含类似


size_t strlen(const char *); / *普通函数* /

#define strlen _builtin_strlen / *编译魔术* /


你可以避免这个潜在的问题:

#include< string.h>

#undef strlen / *以防万一* /

#define strlen my_strlen


这适用于标准功能吗?



是:您引用的标准部分涉及

标准库函数。


而且,如果在定义了funcY函数的标题之外定义是

怎么办?如果我在
中使用它在源文件中怎么办?



对不起;我不明白你的问题。在第一个

部分,你似乎在谈论两种不同的定义,

,我不确定每个人所指的是什么。 在源文件中使用它

也令人困惑:首先,它不清楚它是什么?意思是,第二,

但是在源文件中你可以使用* any * C构造吗?


-

Eric Sosman
es*****@acm-dot-org.inva lid

-

Eric Sosman
es **** *@acm-dot-org.inva 盖子


Eric Sosman写道:


Sensei写道:


>嗨!我关注这样一个定义的合法性:

#define funcX功能

其中funcX属于* standard * C函数。
这是合法的吗?标准说标题中声明的任何函数可以另外实现为标题中定义的宏,因此如果包含标题,则不应明确声明库函数。



如果相应的标题

已经#define''d funcX并且你的funcY没有发生,那么程序可能无法编译br />
完全匹配现有定义。例如


#include< string.h>

#define strlen my_strlen


可能会失败,因为< string.hmay已经包含类似


size_t strlen(const char *); / *普通函数* /

#define strlen _builtin_strlen / *编译魔术* /


你可以避免这个潜在的问题:

#include< string.h>

#undef strlen / *以防万一* /

#define strlen my_strlen



这是标准C中不允许的。它由7.1.3p2覆盖:

没有其他标识符被保留。如果程序在保留的上下文中声明或定义

标识符(除了

7.1.4允许的除外) ,或者将一个保留的标识符作为宏名称,行为是

unde ??? ned。

更实际的是,定义一个与标准名称相同的宏

库函数可以破坏其他尝试调用标准

函数的宏。


>这适用于标准功能吗?



是:您引用的标准部分涉及

标准库函数。


>而且,如果定义是在定义了funcY函数的标题之外的定义,以及如果我在源文件中使用它会怎么样?



对不起;我不明白你的问题。在第一个

部分,你似乎在谈论两种不同的定义,

,我不确定每个人所指的是什么。 在源文件中使用它

也令人困惑:首先,它不清楚它是什么?意思是,第二,

但是在源文件中你可以使用* any * C构造吗?



我也很困惑,我能想到的最好的是:


x1.c:< br $>
#define printf dummy

extern void printf(void);

int main(void){

printf( );

}


x2.c:

#include< stdio.h>

void dummy(void){

puts(Hello,world!);

}


如果这样意思是,我没有看到任何禁止它的东西。这是非常差的

风格,但并非无效。


On 2006-08-21 14:36:05 +0200,Harald van D?3k< tr ***** @ gmail.comsaid:


这是标准C中不允许的。它由7.1覆盖.3p2:

"没有保留其他标识符。如果程序在保留它的上下文中声明或定义了一个

标识符(除了

7.1.4允许的标识符),或者将保留标识符定义为宏名称,行为是

undefined。"

更实际上,定义一个与标准名称相同的宏

库函数可以打破其他尝试调用标准

函数的宏。



您能否澄清上述声明?阅读下一篇:


>对不起;我不明白你的问题。在第一部分中,您似乎在谈论两种不同的定义,而且我不确定每个人所指的是什么。 在源文件中使用它同样令人困惑的是:首先,目前还不清楚它是什么。意思是,第二,
在源文件中你可以使用* any * C构造吗?


我也很困惑,我能想到的最好的是:


x1.c:

#define printf dummy

extern void printf(void);

int main(void){

printf();

}


x2.c:

#include< stdio.h> ;

void dummy(void){

puts(Hello,world!);

}


如果是这个意思,我不会看到任何禁止它的东西。这是非常差的

风格,但不是无效的。



是的,这就是我的意思,在源文件中使用#define只需

就像你写的那样。我知道它很差,但它是我找到的东西:)


澄清我的顾虑,我有一个非标准的C库,并不是全部
它应该具有的功能,在我发现的示例代码中,因为对于

实例,它们没有printf或strlen,它们使用预编译器魔法:


#include< stdio.h>


#define printf _sdk_PRINT_TO_FOO

#define strlen _sdk_STRING_LENGTH_BAR


int main(无效)

{

printf(" Hello,world!\ n");

返回0; < br $>
}


#define是*里面*应用程序代码,而不是libc。

(伪)libc代码有_sdk_PRINT_TO_FOO和_sdk_STRING_LENGTH_BAR

correclty(我希望)工作。我发现这个来源很奇怪,而且我不知道这些定义的合法性。

-

Sensei< se******@mac.com>


乐观主义者认为这是所有可能世界中最好的。

悲观主义者担心这是真的。 [J. Robert Oppenheimer]


Hi! I''m concerned about the legality of such a definition:

#define funcX funcY

where funcX belongs to the *standard* C functions. Is it legal to do
this? The standard says "any function declared in a header may be
additionally implemented as a macro defined in the header, so a library
function should not be declared explicitly if its header is included".

Is this applicable to standard functions? And, what if the definition
is outside the header where the funcY function is defined, and what if
I use it in a source file?

Thanks!

--
Sensei <se******@mac.com>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]

解决方案

Sensei wrote:

Hi! I''m concerned about the legality of such a definition:

#define funcX funcY

where funcX belongs to the *standard* C functions. Is it legal to do
this? The standard says "any function declared in a header may be
additionally implemented as a macro defined in the header, so a library
function should not be declared explicitly if its header is included".

The program may fail to compile if the appropriate header
has already #define''d funcX and your funcY does not happen to
match the existing definition perfectly. For example

#include <string.h>
#define strlen my_strlen

may fail, because <string.hmay already contain something like

size_t strlen(const char*); /* ordinary function */
#define strlen _builtin_strlen /* compiler magic */

You could avoid that potential problem:

#include <string.h>
#undef strlen /* just in case */
#define strlen my_strlen

Is this applicable to standard functions?

Yes: the section of the Standard you quoted concerns the
standard library functions.

And, what if the definition is
outside the header where the funcY function is defined, and what if I
use it in a source file?

I''m sorry; I do not understand your questions. In the first
part you seem to be talking of two different kinds of "definition,"
and I''m not sure what each refers to. "Use it in a source file" is
also confusing: First, it''s not clear what "it" means, and second,
where but in a source file could you use *any* C construct?

--
Eric Sosman
es*****@acm-dot-org.invalid
--
Eric Sosman
es*****@acm-dot-org.invalid


Eric Sosman wrote:

Sensei wrote:

>Hi! I''m concerned about the legality of such a definition:

#define funcX funcY

where funcX belongs to the *standard* C functions. Is it legal to do
this? The standard says "any function declared in a header may be
additionally implemented as a macro defined in the header, so a library
function should not be declared explicitly if its header is included".


The program may fail to compile if the appropriate header
has already #define''d funcX and your funcY does not happen to
match the existing definition perfectly. For example

#include <string.h>
#define strlen my_strlen

may fail, because <string.hmay already contain something like

size_t strlen(const char*); /* ordinary function */
#define strlen _builtin_strlen /* compiler magic */

You could avoid that potential problem:

#include <string.h>
#undef strlen /* just in case */
#define strlen my_strlen

That''s not allowed in standard C. It''s covered by 7.1.3p2:
"No other identi???ers are reserved. If the program declares or de???nes an
identi???er in a context in which it is reserved (other than as allowed by
7.1.4), or de???nes a reserved identi???er as a macro name, the behavior is
unde???ned."
And more practically, defining a macro with the same name as a standard
library function can break other macros which try to call the standard
function.

>Is this applicable to standard functions?


Yes: the section of the Standard you quoted concerns the
standard library functions.

>And, what if the definition is
outside the header where the funcY function is defined, and what if I
use it in a source file?


I''m sorry; I do not understand your questions. In the first
part you seem to be talking of two different kinds of "definition,"
and I''m not sure what each refers to. "Use it in a source file" is
also confusing: First, it''s not clear what "it" means, and second,
where but in a source file could you use *any* C construct?

I''m confused as well, the best I can come up with is:

x1.c:
#define printf dummy
extern void printf(void);
int main(void) {
printf();
}

x2.c:
#include <stdio.h>
void dummy(void) {
puts("Hello, world!");
}

If this is meant, I don''t see anything disallowing it. It''s extremely poor
style, but not invalid.


On 2006-08-21 14:36:05 +0200, Harald van D?3k <tr*****@gmail.comsaid:

That''s not allowed in standard C. It''s covered by 7.1.3p2:
"No other identifiers are reserved. If the program declares or defines an
identifier in a context in which it is reserved (other than as allowed by
7.1.4), or defines a reserved identifier as a macro name, the behavior is
undefined."
And more practically, defining a macro with the same name as a standard
library function can break other macros which try to call the standard
function.

Can you clarify the statement above? Read next:

>I''m sorry; I do not understand your questions. In the first
part you seem to be talking of two different kinds of "definition,"
and I''m not sure what each refers to. "Use it in a source file" is
also confusing: First, it''s not clear what "it" means, and second,
where but in a source file could you use *any* C construct?

I''m confused as well, the best I can come up with is:

x1.c:
#define printf dummy
extern void printf(void);
int main(void) {
printf();
}

x2.c:
#include <stdio.h>
void dummy(void) {
puts("Hello, world!");
}

If this is meant, I don''t see anything disallowing it. It''s extremely poor
style, but not invalid.

Yes, That''s what I meant, using the #define inside a source file just
the way you wrote. I know it''s poor, but it''s what I found :)

Clarifying my concerns, I have a non standard C library that hasn''t all
the functions it should, in the example code I found that since, for
instance, they have no printf or strlen, they use precompiler magic:

#include <stdio.h>

#define printf _sdk_PRINT_TO_FOO
#define strlen _sdk_STRING_LENGTH_BAR

int main(void)
{
printf("Hello, world!\n");
return 0;
}

The #defines are *inside* the application code, not in the libc. The
(pseudo)libc code has _sdk_PRINT_TO_FOO and _sdk_STRING_LENGTH_BAR
correclty (I hope) working though. I found this source quite weird, and
I didn''t know about the legality of such definitions.
--
Sensei <se******@mac.com>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]


这篇关于这#define合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆