title: Preprocessor stringification macro doesn't work
body: <p>I am having a problem getting this COMPARE macro to work.  Any ideas on how to fix?</p>
  
  <p>Its a bit of an artificial sample - I wanted to make it as small as possible.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  
  enum rqtypes {Unknown, Monitor, Query, Snapshot };
  
  class base {
  public:
     base() : type(Unknown) {}
     rqtypes type;
  };
  
  class CBMonitorDeviceRequest : public base
  {
  public:
     CBMonitorDeviceRequest() : dn(0) {}
     char* dn;
  };
  
  
  //I want the equivalent of:
  //        case MonitorDeviceRequestID:
  //           CBMonitorDeviceRequest* pthis = static_cast&lt;CBMonitorDeviceRequest*&gt;(thisrq);
  //           if(pthis &amp;&amp; strcmp(pthis-&gt;dn1, "1234") == 0)
  //              return 0;
  //           else
  //              return -1;
  //           break;
  
  
  int main(int argc, char* argv[])
  {
     CBMonitorDeviceRequest* ptr = new CBMonitorDeviceRequest;
     ptr-&gt;type = Monitor;
     ptr-&gt;dn = new char(strlen("1234") + 1);
     strcpy(ptr-&gt;dn, "1234");
  
  #define COMPARE(id, thismsg) case id##ID: { \
     CB##id * pthis = static_cast&lt;CB##id *&gt;(thismsg); \
     if(pthis &amp;&amp; strcmp(pthis-&gt;dn, "1234") == 0) \
        std::cout &lt;&lt; "found"; \
     else \
        std::cout &lt;&lt; "not found"; \
       break;  } \
  
     switch(ptr-&gt;type){
        COMPARE(Monitor, ptr);
     }
  
  #undef COMPARE
  
      return 0;
  }
  </code></pre>
  
  <p>I get eg:</p>
  
  <pre><code>(46) : error C2065: 'MonitorID' : undeclared identifier
  (46) : error C2051: case expression not constant
  (46) : error C2065: 'CBMonitor' : undeclared identifier
  (46) : error C2065: 'pthis' : undeclared identifier
  (46) : error C2061: syntax error : identifier 'CBMonitor'
  (46) : error C2065: 'pthis' : undeclared identifier
  (46) : error C2065: 'pthis' : undeclared identifier
  (46) : error C2227: left of '-&gt;dn' must point to class/struct/union/generic type
  </code></pre>
  
  <p>Using gcc -E I get:
      # 30 "macro_fun2.cpp"
      int main(int argc, char* argv[])
      {
         CBMonitorDeviceRequest* ptr = new CBMonitorDeviceRequest;
         ptr->type = Monitor;
         ptr->dn = new char(strlen("1234") + 1);
         strcpy(ptr->dn, "1234");
      # 45 "macro_fun2.cpp"
         switch(ptr->type){
            case MonitorID: { CBMonitor * pthis = static_cast(ptr); if(pthis &amp;&amp; strcmp(pthis->dn, "1234") == 0) Monitor = Query; else Monitor = Snapshot;
       break; };
     }</p>
  
  <p>return 0;
  }</p>
  
  <pre><code>*** By the way I changed code to to avoid the massive printing of iostream by preprocessor - otherwise -E printing would have been huge.
  
  #define COMPARE(id, thismsg) case id##ID: { \
     CB##id * pthis = static_cast&lt;CB##id *&gt;(thismsg); \
     if(pthis &amp;&amp; strcmp(pthis-&gt;dn, "1234") == 0) \
        id = Query; \
     else \
        id =  Snapshot; \
       break;  } \
  </code></pre>