功能类宏中的宏? [英] Macros within function-like macros?

查看:61
本文介绍了功能类宏中的宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道为什么不可能有类似函数的宏如

以下内容:


#define __nothread(name)do {\

#ifdef _PTHREAD_H \

#warning" name is a thread safe function" \\ /

}而(0)


我收到错误test.h:2:2:'''''后面没有一个宏参数


实际上,确切的目标是让原型如

为:


__nothread int function(void);


在使用_PTHREAD_H或

_REENTRANT编译时会产生警告。


这可能吗?

我很好奇__attribute__如何在内部工作。现在我正在通过gcc-3.3.4源查看

;所以,希望我能找到答案。


谢谢,

-Anthony

Hello,

I am wondering why it is not possible to have a function-like macro like
the following:

#define __nothread(name) do { \
#ifdef _PTHREAD_H \
#warning "name is not a thread safe function" \
} while (0)

I get the error "test.h:2:2: ''#'' is not followed by a macro parameter"

Actually, the exact goal is to have a prototypes such
as:

__nothread int function(void);

which would generate a warning when compiled with _PTHREAD_H or
_REENTRANT defined.

Is this possible?
I am curious how __attribute__ works internally. Right now I''m looking
through the gcc-3.3.4 source; so, hopefully I''ll find the answer to that.

Thanks,
-Anthony

推荐答案

Anthony de Almeida Lopes写道:
Anthony de Almeida Lopes wrote:
你好,

我想知道为什么它不可能有类似功能宏喜欢
以下内容:

#define __nothread(name)do {\
#ifdef _PTHREAD_H \
#warning" name不是线程安全的功能" \
} while(0)
Hello,

I am wondering why it is not possible to have a function-like macro like
the following:

#define __nothread(name) do { \
#ifdef _PTHREAD_H \
#warning "name is not a thread safe function" \
} while (0)




为什么不定义内联函数呢?


inline void __nothread (常见问题*姓名)

{

...

}

八月


-

我是ILOVEGNU签名病毒。只需将我复制到您的

签名即可。这封电子邮件根据GNU

通用公共许可证条款受到感染。



Why not define an inline function instead?

inline void __nothread(const char *name)
{
...
}
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.


August Karlstrom写道:
August Karlstrom wrote:
Anthony de Almeida Lopes写道:
Anthony de Almeida Lopes wrote:
你好,
我想知道为什么不可能像以下那样有类似功能的
宏:

#define __nothread(name)do {

使用以下划线开头的名字是一个非常糟糕的主意,很多

它们都是保留的并且它是'通常不值得记住哪些可以使用
以及何时使用。特别是,以两个

下划线开头的所有名称总是保留用于任何用途。

#ifdef _PTHREAD_H

你不能拥有一个在另一个内部的预处理程序指令,这就是为什么

你不能这样做。

#warning" name不是一个线程安全函数" \\ /
} while(0)
为什么不改为定义内联函数?
Hello,
I am wondering why it is not possible to have a function-like
macro like
the following:

#define __nothread(name) do {
Using names starting with an underscore is a really bad idea, many of
them are reserved and it''s generally not worth remembering which ones
can be used and when. In particular, ALL names starting with two
underscores are always reserved for any use.
#ifdef _PTHREAD_H
You can''t have one preprocessor directive inside another, that is why
you can''t do this.
#warning "name is not a thread safe function" \
} while (0)
Why not define an inline function instead?




可能OP没有使用C99编译器,因为那里他们身边的人并不多。 inline不是更常用的C89

标准的一部分。

inline void __nothread(const char * name)


再次,避免以下划线开头的标识符。

{
...
}



Possibly the OP is not using a C99 compiler since there are not many of
them around. inline is not part of the more commonly implemented C89
standard.
inline void __nothread(const char *name)
Again, avoid identifiers starting with underscores.
{
...
}



-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,但它是真实的,我读了它。


--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


Anthony de Almeida Lopes写道:
Anthony de Almeida Lopes wrote:
你好,

我想知道为什么不可能有类似函数的宏如下


#define __nothread(name)do {\
#ifdef _PTHREAD_H \
#warning" name不是线程安全函数 \\ n
} while(0)

我收到错误test.h:2:2:'''''后面没有宏参数


因为生成的[...]令牌序列未被处理

作为预处理指令,即使它类似于一个[...] ;

(ISO / IEC 9899:199,第6.10.3.4节第3段)。换句话说,

因为宏扩展无法产生预处理指令。


也可能会注意到C没有#warning指令。

行是合法的,但是是非指令行。 (第6.1节,第1段)。

实际上,确切的目标是有一个原型如下:

__nothread int function(void); _REENTRANT定义编译时会产生警告。

这可能吗?


您可以这样做(但请参阅Flash Gordon'

关于保留标识符的回复):


#if定义_PTHREAD_H ||已定义_REENTRANT

#define __nothread>>> 不是线程安全的! <<<

#else

#define __nothread / * empty * /

#endif

__nothread int function(void);


....但我有预感,你想要的东西有点不同

来自你所描述的。我的猜测是,当使用不需要的宏编译声明时,你不需要

错误,但是当使用不受欢迎的宏编译代码时会调用

声明的函数。我能想到的最好的问题

就像


int function(void);

double麻烦(int boil);

...

#if定义_PTHREAD_H ||已定义_REENTRANT

#define function>>> 不是线程安全的! <<<

#define trouble>>> 不是线程安全的! <<<<

...

#endif

我很好奇__attribute__如何在内部工作。现在我正在通过gcc-3.3.4来源查看
;所以,希望我能找到答案。
Hello,

I am wondering why it is not possible to have a function-like macro like
the following:

#define __nothread(name) do { \
#ifdef _PTHREAD_H \
#warning "name is not a thread safe function" \
} while (0)

I get the error "test.h:2:2: ''#'' is not followed by a macro parameter"
Because "The resulting [...] token sequence is not processed
as a preprocessing directive even if it resembles one [...]"
(ISO/IEC 9899:199, section 6.10.3.4 paragraph 3). In other words,
because a macro expansion cannot produce a preprocessing directive.

It might also be noted that C has no #warning directive. The
line is legal, but is a "non-directive" (section 6.1, paragraph 1).
Actually, the exact goal is to have a prototypes such
as:

__nothread int function(void);

which would generate a warning when compiled with _PTHREAD_H or
_REENTRANT defined.

Is this possible?
You could do something like this (but see Flash Gordon''s
response about reserved identifiers):

#if defined _PTHREAD_H || defined _REENTRANT
#define __nothread >>> "Not thread-safe!" <<<
#else
#define __nothread /* empty */
#endif
__nothread int function(void);

.... but I have a hunch you want something a little different
from what you''ve described. My guess is that you don''t want an
error when the declaration is compiled with the undesired macros
defined, but when code compiled with the unwelcome macros calls
the declared function. The best I can think of for that problem
is something along the lines of

int function(void);
double trouble(int boil);
...
#if defined _PTHREAD_H || defined _REENTRANT
#define function >>> "Not thread-safe!" <<<
#define trouble >>> "Not thread-safe!" <<<
...
#endif
I am curious how __attribute__ works internally. Right now I''m looking
through the gcc-3.3.4 source; so, hopefully I''ll find the answer to that.




__attribute__不是C的一部分。在gnu论坛中提问。


-

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



__attribute__ is not part of C. Ask in a gnu forum.

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


这篇关于功能类宏中的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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