Pass a token with "::" as argument to C/C++ macro
<p>Suppose I have two structs with the same member variables but different "prefixes". One is in a namespace and the other is prefixed with a certain token.</p>

<p>I want to write a macro to do the same operation on these structs which accepts the differing prefix as an input. I tried this:</p>

<pre><code>#include &lt;cstdio&gt;

struct A__foo_
{
  int bar;
} typedef A__foo;

namespace B {
  struct foo {
    int bar;
  };
}

#define GET_BAR(Prefix)\
  { \
    Prefix ## foo my_foo;\
    printf("Bar is: %d", my_foo.bar);\
  }

int main(int argc, char ** argv) {
  GET_BAR(A__);
  GET_BAR(B::);
}
</code></pre>

<p>I get this compiler error:</p>

<blockquote>
  <p>macros_example.cpp:22:7: error: pasting formed '::foo', an invalid preprocessing token
        GET_BAR(B::);</p>
</blockquote>

<p>Is there a way to rewrite this macro in an elegant way to accept the two inputs and concatenate them with "foo"? I've tried pre-processing B:: by concatenating "B" and the double colons. I've also tried changing <code>Prefix ## foo</code> to simply <code>Prefix foo</code> but then calling <code>GET_BAR(A__)</code> causes a compile error.</p>