title: C++ how to #define/rename a nested function?
body: <pre><code>    namespace first {
      namespace second {
          class Third {
              static void foo() {
                  std::cout &lt;&lt; &quot;foo\n&quot;;
              }
          };
      }
      }
  
      void bar() {
          std::cout &lt;&lt; &quot;bar\n&quot;;
      }
  
      #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; &quot;release version\n&quot;;
          }
      };
  
      struct DebugVersion {
          static void foo() {
              std::cout &lt;&lt; &quot;debug version\n&quot;;
          }
      };
  
      #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; &quot;release version\n&quot;;
          }
      };
      
      struct DebugVersion {
          static void foo(const char* file, long line) {
              std::cout &lt;&lt; &quot;debug version\n&quot;;
          }
      };
      }
      }
      #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>