title: Any portable tricks to obtain namespace name in C++?
body: <p>I have some well-formed code looks like this:</p>
  
  <pre><code>NAMESPACE_BEGIN(Foo)
  inline void test() {
      string s = xxx;
  }
  NAMESPACE_END(Foo)
  </code></pre>
  
  <p>So, is there any portable tricks by using the <code>NAMESPACE_BEGIN()</code> macro to obtain namespace name "Foo" in <code>test()</code>?</p>
  
  <p>I'm thinking of something like this, but it would surely cause symbol redefinition:</p>
  
  <pre><code>#define NAMESPACE_BEGIN(x) \
      namespace x { \
          inline const char *_curNamespace() { \
              return #x; \
          }
  #define NAMESPACE_END(x) \
      }
  </code></pre>
  
  <p>There's also a workaround looks like this, but that's not very convenient</p>
  
  <pre><code>#define NAMESPACE_NAME Foo
  // using a header file so that we can use #ifdef guard
  #include "MyNamespaceBegin.h"
  ...
  #include "MyNamespaceEnd.h"
  </code></pre>
  
  <p><strong>EDIT</strong>:</p>
  
  <ul>
  <li><p>Why I need this:</p>
  
  <p>I'm using much of macro to generate codes to achieve some
  dynamic reflection logic (yes, not static template reflection),
  it's all right within class scope by using static member function,
  but does not work for namespaces</p></li>
  <li><p>Why not to manually declare the name getter once:</p>
  
  <p>What I want is something like this:</p>
  
  <pre><code>// the global default version
  const char *_curNamespace() {return "";}
  
  namespace X {
      // the local namespace version
      const char *_curNamespace() {return "X";}
  
      // some verbose reflection register code
      ...
      registerSomething(_curNamespace());
      ...
  }
  </code></pre>
  
  <p>Of course, all of the verbose register code should be generated by macro</p>
  
  <p>And, app level user should not care about the <code>_curNamespace()</code>,
  so, I want to <strong>simplify the user's usage</strong>,
  by using a custom <code>NAMESPACE_BEGIN(xxx)</code> macro <strong>at any case</strong></p></li>
  <li><p>If you are still curious about what I'm doing,
  check this: <a href="https://github.com/ZFFramework/ZFFramework" rel="nofollow noreferrer">https://github.com/ZFFramework/ZFFramework</a></p>
  
  <p>I'm using lots of tricks to achieve fully dynamic reflection in pure C++,
  to achieve some of my fancy thoughts,
  for now, this project is just for fun,
  I have no idea whether it has practicability</p></li>
  </ul>
  
  <p><strong>EDIT2</strong>:</p>
  
  <p>For now, I think the best workaround should be like this:</p>
  
  <pre><code>#define NAMESPACE_BEGIN(ns) \
      namespace ns { \
          extern const char *__curNS();
  #define NAMESPACE_END(ns) \
      }
  #define NAMESPACE_REG(ns) \
          const char *__curNS() {return #ns;}
  </code></pre>
  
  <ul>
  <li>app level users still only need to care about <code>NAMESPACE_BEGIN</code></li>
  <li><code>NAMESPACE_REG</code> must be declared exactly once, in source file
  
  <ul>
  <li>if not, undefined symbol would happen</li>
  <li>if more than once, duplicated symbol would happen</li>
  <li>although it's annoying and sometimes you need additional source file
  to hold the <code>NAMESPACE_REG</code>,
  the strict rule should prevent user from forgetting the ugly workaround</li>
  </ul></li>
  </ul>