Checking if a class has an insertion operator when using the ostream in a class template
<p>I have a class template that uses the <code>iostream</code> to print out some text to the screen.
I want to throw an exception if a class does not have the insertion operator (<code>&lt;&lt;</code>). Is there a technique that I could use to solve this problem?</p>

<p>I looked <a href="http://stackoverflow.com/questions/5768511/using-sfinae-to-check-for-global-operator">here</a> and saw this example that uses the SFINAE coding technique.</p>

<pre><code>struct sfinae_base {
  typedef char yes[1];
  typedef char no[2];
};

template&lt;typename T&gt;
 struct has_insertion_operator : sfinae_base {
  template&lt;typename U&gt; static yes&amp; test(U&amp;);
  template&lt;typename U&gt; static no&amp; test(...);

  static std::ostream &amp;s;
  static T const &amp;t;

  static bool const value = sizeof(test(s&lt;&lt; t)) == sizeof(yes);
};
</code></pre>

<p>I am new to this technique and I am wondering if I have understood the code above correctly:</p>

<p>The decision making part of the code is the value data member of has_insertion_operator structure.<br>
If <code>test(s&lt;&lt;t)</code> is successful, then yes is return from:</p>

<pre><code>template&lt;typename U&gt; static yes&amp; test( U&amp; );
</code></pre>

<p>which causes value to equal <code>true</code>.</p>

<p>If the <code>test(s&lt;&lt;t)</code> is not successful:</p>

<pre><code>template&lt;typename U&gt; static no&amp; test(...);
</code></pre>

<p>No is call and value is equal to <code>false</code>.<br>
If I put the following in an <code>if</code> statement would that be sufficient enough?</p>

<pre><code>if(has_insertion_operator&lt;SomeClass&gt;::value) std::cout &lt;&lt; "PASS" &lt;&lt; std::endl;
</code></pre>
