MACRO很丑,但...... [英] MACROs are ugly but...
问题描述
....在某些情况下,如果不是其他任何东西,它们可能是有用的,
然后至少为了节省大量的重复性改写。 :-)
除了做一些更好的事情之外,我很好奇
以下语法问题是如何解决的。它应该同样适用于C和C ++,因为它主要是与预处理器相关的问题。
我尝试定义类似于以下示例的内容:
-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning(禁用:4995)/ *烦人的警告* / \
\
/ *在这里做一些肮脏的工作(我想隐藏)* / \
\
# pragma警告(默认值:4995)/ *警告重新激活* / \
/ *已完成脏部分* /
------------ -------------------------
当然要点几个地方使用DO_DIRTYWORK
来源。它的内容并不是主要逻辑的一部分,但
只是一个库变通方法需要注入一些代码行
无处不在。 (当图书馆稍后修复时,也许我可以只用
将DO_DIRTYWORK宏重新定义为空!)
但是,这不能编译当然,因为一个预处理指令
需要从一行开始作为第一个非空白#,这与宏的行尾相互冲突
逃脱(\)。显然,在这种特殊情况下我想要实现的是从预处理器的第一次替换中发出的实际预处理器
指令(然后
在使用实际代码的地方进一步预处理。
可以实现这样的目标吗?
如何?
-
- + - Ben - + -
....in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetitititive typing. :-)
Beyond the point of "do something better instead", I''m curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.
I tryed to define something similar to the following example:
-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------
The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)
However, this doesn''t compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro''s end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor''s first replacement (then
further preprocessed in-place where the actual code is used).
Can something like that be achieved?
How?
--
-+-Ben-+-
推荐答案
Ben Hetland写道:
Ben Hetland wrote:
...在某些情况下,如果不是为了其他任何东西,它们可能是有用的,
然后至少可以节省大量的重复性输入。 :-)
除了做一些更好的事情之外,我很好奇如何解决以下句法问题。它应该同样适用于C和C ++,因为它主要是与预处理器相关的问题。
我尝试定义类似于以下示例的内容:
- ------------------------------------
#define DO_DIRTYWORK \
\\ \\#/> #pragma警告(禁用:4995)/ *恼人的警告* / \
\
/ *在这里做一些肮脏的工作(我想隐藏)* / \
\
#pragma警告(默认值:4995)/ *警告重新激活* / \
/ *已完成脏部分* /
---------- ---------------------------
关键是当然要在DO_DIRTYWORK周围使用几个地方
消息来源。它的内容并不是主要逻辑的一部分,但仅仅是一个库变通方法,需要在任何地方注入一些代码行。 (当图书馆稍后修复时,也许我可以将DO_DIRTYWORK宏重新定义为空!)然而,这当然没有编译,因为一个预处理指令
需要从一行开始作为第一个非空白#,这与宏的行尾逃逸(\)冲突。显然,我在这个特殊情况下要实现的是从预处理器的第一次替换中发出实际的预处理器指令(然后在实际的地方进一步预处理)代码被使用了。
可以实现这样的目标吗?
如何?
-
- + - Ben - + -
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetitititive typing. :-)
Beyond the point of "do something better instead", I''m curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.
I tryed to define something similar to the following example:
-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------
The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)
However, this doesn''t compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro''s end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor''s first replacement (then
further preprocessed in-place where the actual code is used).
Can something like that be achieved?
How?
--
-+-Ben-+-
>
把脏东西放进去怎么样?在一个单独的标题中,这样做:
#if定义(DO_DIRTYWORK)
#include" dirtywork.h"
#endif
然后在你的dirtywork.h中找到你的pragma和诸如此类的东西...
-David
>
[删除交叉发布]
2005年10月6日07:06:30 -0700,David Resnick < ln ******** @ gmail.com>
写道:
[cross-posting removed]
On 6 Oct 2005 07:06:30 -0700, "David Resnick" <ln********@gmail.com>
wrote:
Ben Hetland写道:
Ben Hetland wrote:
...在某些情况下,如果不是为了其他任何东西,它们可能是有用的,
然后至少可以节省大量的重复计时。 :-)
除了做一些更好的事情之外,我很好奇如何解决以下句法问题。它应该同样适用于C和C ++,因为它主要是与预处理器相关的问题。
我尝试定义类似于以下示例的内容:
- ------------------------------------
#define DO_DIRTYWORK \
\\ \\#/> #pragma警告(禁用:4995)/ *恼人的警告* / \
\
/ *在这里做一些肮脏的工作(我想隐藏)* / \
\
#pragma警告(默认值:4995)/ *警告重新激活* / \
/ *已完成脏部分* /
---------- ---------------------------
关键是当然要在DO_DIRTYWORK周围使用几个地方
消息来源。它的内容并不是主要逻辑的一部分,但仅仅是一个库变通方法,需要在任何地方注入一些代码行。 (当图书馆稍后修复时,也许我可以将DO_DIRTYWORK宏重新定义为空!)然而,这当然没有编译,因为一个预处理指令
需要从一行开始作为第一个非空白#,这与宏的行尾逃逸(\)冲突。显然,我在这个特殊情况下要实现的是从预处理器的第一次替换中发出实际的预处理器指令(然后在实际的地方进一步预处理)代码被使用了。
可以实现这样的目标吗?
如何?
-
- + - Ben - + -
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetitititive typing. :-)
Beyond the point of "do something better instead", I''m curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.
I tryed to define something similar to the following example:
-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------
The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)
However, this doesn''t compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro''s end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor''s first replacement (then
further preprocessed in-place where the actual code is used).
Can something like that be achieved?
How?
--
-+-Ben-+-
>
如何把脏东西放进去在一个单独的标题中,这样做:
#if定义(DO_DIRTYWORK)
#include" dirtywork.h"
#endif
然后在你的脏工作中有你的pragma和诸如此类的东西...
-David
How about putting the "dirtywork" in a separate header, and doing this:
#if defined(DO_DIRTYWORK)
#include "dirtywork.h"
#endif
Then have your pragmas and whatnot in your dirtywork.h...
-David
对于配置宏来说这是一个非常好的解决方案
为整个项目定义,或者为一个项目的大部分定义。
不幸的是,它并不那么容易。通常人们会想要只用单个函数,或者可能是#pragma warning(禁用...)之间单个源的内容
文件(不是标题!)。 )和
#pragma warning(默认...)或类似的东西。这将
需要至少两个单独的头文件,我认为所涉及的
维护将大于仅仅输入
pragma'是的。
恕我直言的人应该只把这些东西放在需要由多个源文件访问的单独文件中。人们几乎不希望为整个项目关闭特定的警告,尽管肯定有足够的程序员可以反驳。我是一个人,只有在我的代码完全没有任何警告的情况下编译时,我才会感到高兴。如果
是某个警告我无法解决,那么我将其关闭
仅用于代码的任何部分导致警告并记录
很好地说明为什么要关掉它。输入#pragma或
无论直接进入源代码都会强制执行他们非常本地化的成语,并且他们之间存在强烈的依赖关系
及其影响的代码。
无论如何,常用的#pragmas可以很容易地存储为模板
在大多数现代IDE中是为了节省打字。
-
Bob Hairgrove
没有********** @ Home.com
Ben Hetland写道:
Ben Hetland wrote:
...在某些情况下,如果不是为了其他任何东西,它们可能是有用的,
然后至少可以节省大量的重复性输入。 :-)
除了做一些更好的事情之外,我很好奇如何解决以下句法问题。它应该同样适用于C和C ++,因为它主要是与预处理器相关的问题。
我尝试定义类似于以下示例的内容:
- ------------------------------------
#define DO_DIRTYWORK \
\\ \\#/> #pragma警告(禁用:4995)/ *恼人的警告* / \
\
/ *在这里做一些肮脏的工作(我想隐藏)* / \
\
#pragma警告(默认值:4995)/ *警告重新激活* / \
/ *已完成脏部分* /
---------- ---------------------------
关键是当然要在DO_DIRTYWORK周围使用几个地方
消息来源。它的内容并不是主要逻辑的一部分,但仅仅是一个库变通方法,需要在任何地方注入一些代码行。 (当图书馆稍后修复时,也许我可以将DO_DIRTYWORK宏重新定义为空!)然而,这当然没有编译,因为一个预处理指令
需要从一行开始作为第一个非空白#,这与宏的行尾逃逸(\)冲突。显然,我在这个特殊情况下要实现的是从预处理器的第一次替换中发出实际的预处理器指令(然后在实际的地方进一步预处理)代码被使用了。
可以实现这样的目标吗?
如何?
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetitititive typing. :-)
Beyond the point of "do something better instead", I''m curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.
I tryed to define something similar to the following example:
-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------
The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)
However, this doesn''t compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro''s end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor''s first replacement (then
further preprocessed in-place where the actual code is used).
Can something like that be achieved?
How?
您应该能够使用_Pragma预处理器运算符使用宏创建
#pragma指令。 _Pragma是C99标准的一部分
,我不确定它在C ++中的当前状态。但是很多C ++编译器实现了它。
Greg
You should be able to use the _Pragma preprocessor operator to create
#pragma directives using macros. _Pragma is part of the C99 standard
and I am unsure of its current status in C++. But many C++ compilers
implement it.
Greg
这篇关于MACRO很丑,但......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!