删除遗留代码中的宏
我有很多使用以下形式的宏的遗留代码:
#define FXX(x) pField->GetValue(x)
宏强制变量 pField 位于范围内:
.....
FIELD *pField = ....
.....
int i = FXX(3);
int j = FXX(5);
有没有办法在不触及用户代码的情况下替换宏?
由于 FXX(x) 具有函数调用风格,因此我考虑了内联函数或类似的东西。
UPD: 人们只是习惯了宏观,而我想保持原样。
I have a lot of legacy code using macro of the form:
#define FXX(x) pField->GetValue(x)
The macro forces variable pField be in the scope:
.....
FIELD *pField = ....
.....
int i = FXX(3);
int j = FXX(5);
Is there way to replace the macro, without touching user code?
Since FXX(x) has a function invocation style, I thought about inline function or something similar.
UPD:
People just used to the macro, and I want to remain it as is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 find & 怎么样?在您最喜欢的编辑器中替换 函数...我认为它在您在问题中给出的示例中可以正常工作。将
FXX
替换为pField->GetValue
,然后删除#define
行How about using a find & replace function in your favorite editor...I think it would work fine in the example you gave in your question. Replace
FXX
withpField->GetValue
and then remove the#define
linepField
是什么(除了 Systems Hungarian 的一个令人厌恶的好例子之外)?如果碰巧它是一个全局变量或单例或我们只需要其中之一的东西,我们可以做一个像这样的漂亮技巧:将
int
类型更改为您需要它进行操作的任何类型,甚至是模板(如果您需要它支持多种类型)。@epatel 建议的另一种选择是使用您最喜欢的文本编辑器的查找和替换,只需将所有
FFX(x)
行更改为pField->GetValue(x)
,从而消除代码中的宏调用。如果你想保留函数调用,你可以将FFX(x)
更改为FFX(pField, x)
并将宏更改为采用两个参数(或将其更改为一个带有两个参数的函数)。但此时您也可以删除宏。第三种选择是不修复未损坏的部分。该宏不是特别好,但是尝试删除它可能会带来更大的问题。宏并不是撒旦的产物(尽管它至少在地狱里有一些亲戚)。
What is
pField
(besides a fine example of the abomination that is Systems Hungarian)? If, by chance, it's a global variable or a singleton or something that we only need one of, we could do a nifty trick like this:Change the
int
types to whatever types you need it to operate on, or even a template if you need it to support multiple types.Another alternative, suggested by @epatel, is to use your favorite text editor's find-and-replace and just change all the
FFX(x)
lines topField->GetValue(x)
, thus eliminating the macro invokation in your code. If you want to keep a function invokation, you culd changeFFX(x)
toFFX(pField, x)
and change the macro to take two arguments (or change it to a function that takes two arguments). But you might as well just take out the macro at that point.A third alternative, is not to fix that which is not broken. The macro isn't particularly nice, but you may introduce greater problems by trying to remove it. Macros aren't the spawn of Satan (though this one has at least a few relatives in hell).
您需要的是一个依赖于所定义变量的函数。唯一的方法是在与函数相同的作用域中声明该变量。但随后您的函数将使用该函数,而不是从调用函数的位置声明的函数。
所以我相当有信心这是不可能完成的。
What you need is a function that relies on a variable being defined. The only way to do that is to declare that variable in the same scope as the function. But then your function would use that one instead of the one declared from where your function is called.
So I'm fairly confident it can't be done.
好吧,如果您可以将此函数定义放在 pField 已在范围内的位置:
否则,无法在不影响现有代码的情况下将 pField 放入函数中。
在这种情况下,使用宏可能是最好的选择。宏可能是邪恶的,但有时它们是必要的。请参阅 http://www.parashift.com/c++ -faq-lite/big-picture.html#faq-6.15
Well, if you can put this function definition where pField is already in scope:
Otherwise, there's no way to get pField into the the function without affecting existing code.
This may be a case where using the macro is the best alternative. Macros may be evil, but they are sometimes necessary. See http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.15
我会保留原样,但只是为了讨论,并且根据代码的哪些部分是“不可触及的”,您可以定义一个接受 pField 的函子,并在同一范围内创建变量后进行初始化:
但是我坚持认为,为了工作代码而改变它不会真正增加任何价值是没有用的。
I would leave it as it is, but just for the sake of discussion, and depending on what parts of the code are 'untouchable' you could define a functor that takes a pField and initialize after the variable is created in the same scope:
But I insist in that changing working code for the sake of it when it will not really add any value is useless.