简体   繁体   English

C ++如何#define /重命名嵌套函数?

[英]C++ how to #define/rename a nested function?

    namespace first {
    namespace second {
        class Third {
            static void foo() {
                std::cout << "foo\n";
            }
        };
    }
    }

    void bar() {
        std::cout << "bar\n";
    }

    #define first::second::Third::foo bar//this doesn't work

so, what's the correct way to map a nested function to another one? 那么,将嵌套函数映射到另一个函数的正确方法是什么?

Update: 更新:

a more similar situation is: 更类似的情况是:

    struct ReleaseVersion {
        static void foo() {
            std::cout << "release version\n";
        }
    };

    struct DebugVersion {
        static void foo() {
            std::cout << "debug version\n";
        }
    };

    #ifdef _DEBUG 
    #define ReleaseVersion::foo DebugVersion::foo
    #else
    #define DebugVersion::foo ReleaseVersion::foo
    #endif

what I want to do is just like malloc and _malloc_dbg, when #define _CRTDBG_MAP_ALLOC, in debug mode, malloc will be mapped to _malloc_dbg, and in release mode, _malloc_dbg will be mapped to malloc 我想要做的就像malloc和_malloc_dbg,当#define _CRTDBG_MAP_ALLOC,在调试模式下,malloc将映射到_malloc_dbg,在释放模式下,_malloc_dbg将映射到malloc

Update again 再次更新

a more more similar situation is: 更类似的情况是:

    namespace first {
    namespace second {
    struct ReleaseVersion {
        static void foo() {
            std::cout << "release version\n";
        }
    };

    struct DebugVersion {
        static void foo(const char* file, long line) {
            std::cout << "debug version\n";
        }
    };
    }
    }
    #ifdef _DEBUG 
    #define ReleaseVersion::foo() DebugVersion::foo(__FILE__, __LINE__)
    #else
    #define DebugVersion::foo(file, line) ReleaseVersion::foo()
    #endif

so, these 2 version of functions may have different parameters, I cannot just call one. 因此,这两个版本的函数可能具有不同的参数,我不能只调用一个。 I know I can just do this 我知道我可以做到这一点

    #ifdef _DEBUG 
    #define Foo() first::second::DebugVersion::foo(__FILE__, __LINE__)
    #else
    #define Foo() first::second::ReleaseVersion::foo()

but in this way, I must always use Foo(), even in the final release mode, it's still a macro. 但是以这种方式,我必须始终使用Foo(),即使在最终发布模式下,它仍然是一个宏。 I want to know if there are more flexible way to to do this. 我想知道是否有更灵活的方法来执行此操作。

One Solution 一种解决方案

    #ifdef _DEBUG 
    #define foo() foo(__FILE__, __LINE__)
    #define ReleaseVersion DebugVersion
    #else
    #define foo(file, line) foo()
    #define DebugVersion ReleaseVersion
    #endif


    int main() {
        first::second::DebugVersion::foo(__FILE__, __LINE__);
        first::second::ReleaseVersion::foo();
        return 0;
    }

it may be dangerous when there are another foo() or RealeaseVersion/DebugVersion in other namespaces, but if you can make sure there won't, I think it can be an acceptable solution. 在其他命名空间中还有另一个foo()或RealeaseVersion / DebugVersion时,这可能很危险,但是如果可以确保不会,我认为这是可以接受的解决方案。

Your #define is the wrong way around: 您的#define是错误的处理方法:

#define bar first::second::Third::foo

means that bar will be replaced by first::second::Third::foo , which, I believe, is what you want. 意味着bar将被first::second::Third::foo代替,我相信这是您想要的。

This is the opposite of typedef , where things are the other way around. 这与typedef相反,其他情况则相反。

I'm not exactly sure what you want, but this works: 我不确定您想要什么,但这可以工作:

namespace first {
namespace second {
    class Third {
        public: static void foo() {
            std::cout << "foo\n";
        }
    };
}
}

#define bar first::second::Third::foo

int main()
{
  bar();
}

The way malloc / free works is by a macro replacement: malloc / free工作方式是通过宏替换:

#ifdef WANT_DEBUG_MALLOC
#define malloc(x) debug_malloc(x, __FILE__, __LINE__)
#define free(x)   debug_free(x, __FILE__, __LINE__)
#endif

When the preprocessor sees struct foo *p = malloc(sizeof(struct foo) * 10); 当预处理器看到struct foo *p = malloc(sizeof(struct foo) * 10); it will replace it with struct foo *p = debug_malloc(sizeof(struct foo) * 10, "myfile.c", 103); 它将替换为struct foo *p = debug_malloc(sizeof(struct foo) * 10, "myfile.c", 103);

However, as mentioned above, you can't really use namespaces when doing the macro replacement. 但是,如上所述,进行宏替换时不能真正使用名称空间。 You have to EITHER replace the namespace alone, or replace the function name alone. 您必须单独替换名称空间,或者单独替换函数名称。 Of course, it's possible to have two macros, one to substitute the namespace, and one for substituting the function name. 当然,可能有两个宏,一个宏用来替换命名空间,一个宏用来替换函数名称。 But it gets pretty messy pretty quickly, so best avoided, I'd say. 但这很快就会变得很混乱,所以最好避免。

I would rather using inline functions 我宁愿使用内联函数

#ifdef _DEBUG 
static inline void DoFoo() { DebugVersion::foo(); }
#else
static inline void DoFoo() { ReleaseVersion::foo(); }
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM