title: GCC: Customizing printf for string output
body: <p>GCC <a href="http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html" rel="nofollow">allows customization of printf specifiers.</a> However, I don't see how I can "teach" it to accept my string class for <code>%s</code> specifier. My string class is a simple wrapper over char pointer and has exactly one member variable (char * data) and no virtual functions. So, it's kind of ok to pass it as-is to printf-like functions in place of regular <code>char *</code>. The problem is that on gcc static analyzer prevents me from doing so and I have to explicitly cast it to <code>const char *</code> to avoid warnings or errors.</p>
  
  <p>My cstring looks something like this:</p>
  
  <pre><code>class cstring
  {
     cstring() : data(NULL){}
     cstring(const char * str) : data(strdup(str)){}
     cstring(const cstring &amp; str) : data(strdup(str.data)){}
     ~cstring()
     {
          free(data);
     }
     ...
     const char * c_str() const
     {
          return data;
     }
  
  private:
     char * data;
  };
  </code></pre>
  
  <p>Example code that uses cstring:</p>
  
  <pre><code>cstring str("my string");
  printf("str: '%s'", str);
  </code></pre>
  
  <p>On GCC I get this error:<br/>
  error: cannot pass objects of non-trivially-copyable type 'class cstring' through '...'<br/>
  error: format '%s' expects argument of type 'char*', but argument 1 has type 'cstring' [-Werror=format]<br/>
  cc1plus.exe: all warnings being treated as errors<br/></p>