我需要#undef一个本地#define吗?有没有一个本地定义的东西? [英] Do I need to #undef a local #define? Is there such a thing as a local define?

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

问题描述

有时为了使写入和读取更容易,我在函数(例如,#define O_REAL Ogre :: Real)中编写一些本地 #define

Sometimes to make things easier to write and read, I write some local #define macros within functions (for example, #define O_REAL Ogre::Real).

我需要 #undef 本地 #define 才能确保它位于某个代码块中?或者当超出范围时,它是否自动 #undef

Do I need to #undef the local #define to ensure that it stays within a certain block of code? Or does it automatically #undef when it goes out of scope? Does it even have a concept of scope?

我不确定 #define 在这种情况下是如何工作的。现在,我已经实验了代码并得出了一些结论,但由于我不确定,我想要一些专家的意见/建议。

I am unsure on how #define works in this case. Now, I have of course experimented with the code and reached certain conclusions but since I am unsure, I would like some expert opinion/advice.

推荐答案

#define 不尊重任何C ++作用域。没有诸如local #define 这样的东西。它会生效,直到 #undef -ed。预处理器的宏机制就像大多数文本编辑器中的查找和替换功能;

#define does not respect any C++ scope. There is no such thing as a "local" #define. It'll be in effect until it is #undef-ed. The preprocessor's macro mechanism is just like the "find-and-replace" functionality found in most text editors; it has no respect for the contents of the file.

换句话说,如果你想让 #define 在一个特定的代码块内局部化,你必须在该块结束处 #undef ,因为宏不理解范围。

In other words, if you want your #define to be local within a certain block of code, you must #undef it at the end of that block due to the fact that macros do not "understand" scope.

事实上,这是宏的最大原因之一,除非他们是绝对必要的C ++。这是为什么宏名称通常在 UPPER_CASE 中输入,表示它实际上是一个宏。

In fact, that's one of the biggest reasons why macros are discouraged unless they are absolutely necessary in C++. It's why macro names are usually typed in UPPER_CASE to indicate that it's in fact a macro.

对于你的具体情况,实际上有很多无宏的解决方案。请考虑以下内容:

There are actually quite a few macro-less solutions for your specific situation. Consider the following:

namespace ReallyLongOuterNamespace
{
    namespace ReallyLongInnerNamespace
    {
        class Foo {};
        void Bar() {}
    };
}

void DoThis()
{
    // Too much typing!
    ReallyLongOuterNamespace::ReallyLongInnerNamespace::Foo f;
    ReallyLongOuterNamespace::ReallyLongInnerNamespace::Bar();
}

您可以使用命名空间别名

void DoThis()
{
    namespace rlin = ReallyLongOuterNamespace::ReallyLongInnerNamespace;

    rlin::Foo f;
    rlin::Bar();
}

您也可以使用 typedef s:

void DoThis()
{
    typedef ReallyLongOuterNamespace::ReallyLongInnerNamespace::Foo MyFoo;

    MyFoo f;
}

您也可以使用使用声明:

void DoThis()
{
    using ReallyLongOuterNamespace::ReallyLongInnerNamespace::Foo;
    using ReallyLongOuterNamespace::ReallyLongInnerNamespace::Bar;

    Foo f;
    Bar();
}

您甚至可以使用上述的组合!

You can even use a combination of the above!

void DoThis()
{
    namespace rlin = ReallyLongOuterNamespace::ReallyLongInnerNamespace;
    typedef rlin::Foo MyFoo;
    using rlin::Bar;

    MyFoo f;
    Bar();
}

对于 Ogre :: Real float double ,它似乎是 typedef / code>。您仍然可以使用命名空间别名, typedef s和使用声明 typedef s以及:

With respect to Ogre::Real, it appears to be a typedef for a float or a double. You can still use namespace aliases, typedefs and using declarations with typedefs as well:

void UseOgre()
{
    typedef Ogre::Real o_Real; // Yes, you can typedef typedefs.
    using Ogre::Real;
    /* Or, you can use:
    namespace o = Ogre;
    typedef o::Real o_Real;
    using o::Real;
    */

    // All equivalent
    Ogre::Real r1;
    o_Real r2;
    Real r3;
    o::Real r4;
}

这篇关于我需要#undef一个本地#define吗?有没有一个本地定义的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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