Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ how to #define/rename a nested function?
    primarykey
    data
    text
    <pre><code> namespace first { namespace second { class Third { static void foo() { std::cout &lt;&lt; "foo\n"; } }; } } void bar() { std::cout &lt;&lt; "bar\n"; } #define first::second::Third::foo bar//this doesn't work </code></pre> <p>so, what's the correct way to map a nested function to another one?</p> <h1>Update:</h1> <p>a more similar situation is:</p> <pre><code> struct ReleaseVersion { static void foo() { std::cout &lt;&lt; "release version\n"; } }; struct DebugVersion { static void foo() { std::cout &lt;&lt; "debug version\n"; } }; #ifdef _DEBUG #define ReleaseVersion::foo DebugVersion::foo #else #define DebugVersion::foo ReleaseVersion::foo #endif </code></pre> <p>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</p> <h1>Update again</h1> <p>a more more similar situation is:</p> <pre><code> namespace first { namespace second { struct ReleaseVersion { static void foo() { std::cout &lt;&lt; "release version\n"; } }; struct DebugVersion { static void foo(const char* file, long line) { std::cout &lt;&lt; "debug version\n"; } }; } } #ifdef _DEBUG #define ReleaseVersion::foo() DebugVersion::foo(__FILE__, __LINE__) #else #define DebugVersion::foo(file, line) ReleaseVersion::foo() #endif </code></pre> <p>so, these 2 version of functions may have different parameters, I cannot just call one. I know I can just do this</p> <pre><code> #ifdef _DEBUG #define Foo() first::second::DebugVersion::foo(__FILE__, __LINE__) #else #define Foo() first::second::ReleaseVersion::foo() </code></pre> <p>but in this way, I must always use Foo(), even in the final release mode, it's still a macro. I want to know if there are more flexible way to to do this.</p> <h1>One Solution</h1> <pre><code> #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; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload