Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create wrappers which will return some object instead of ostream&amp; which will handle printing. In your case it will object for printing humand-readable value and object for printing database code. Here's rough example which prints human-readable form and integer form. <code>ostream_enum_wrapper_human</code> class with its operator &lt;&lt; is used for printing human-readable form, class <code>ostream_enum_wrapper_int</code> with its &lt;&lt; is used for printing integer code. To switch from ostream&amp; to wrapper, operator &lt;&lt; (ostream&amp;, wrappertag) is used, which wraps ostream object inside of wrapper and returns wrapped object. So next &lt;&lt; operator is called on wrapper object, not on ostream&amp;, and wrapper class knows how to print value.</p> <pre><code>#include &lt;iostream&gt; using namespace std; class ostream_enum_wrapper_human { public: ostream&amp; out; ostream_enum_wrapper_human(std::ostream&amp; _out) : out(_out){} }; class ostream_enum_wrapper_int { public: std::ostream&amp; out; ostream_enum_wrapper_int(std::ostream&amp; _out) : out(_out){} }; enum T{zero,one,two}; struct human_readable{} HumanReadable; ostream_enum_wrapper_human operator &lt;&lt; (ostream&amp; out, human_readable){ return ostream_enum_wrapper_human(out); } struct print_int{} PrintInt; ostream_enum_wrapper_int operator &lt;&lt; (ostream&amp; out, print_int){ return ostream_enum_wrapper_int(out); } ostream&amp; operator &lt;&lt; (ostream_enum_wrapper_human out, T t) { switch(t) { case zero: out.out &lt;&lt; "zero"; break; case one: out.out &lt;&lt; "one"; break; case two: out.out &lt;&lt; "two"; break; } return out.out; } ostream&amp; operator &lt;&lt; (ostream_enum_wrapper_int out, T t) { return out.out &lt;&lt; static_cast&lt;int&gt;(t); } int main() { cout &lt;&lt; HumanReadable &lt;&lt; zero &lt;&lt; PrintInt &lt;&lt; zero &lt;&lt; HumanReadable &lt;&lt; two; } </code></pre> <p><a href="http://codepad.org/AgTYbvqK" rel="nofollow noreferrer">prints zero0two</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload