title: How can I work around Visual Studio 2012 not supporting explicit conversion operators?
body: <p>Here's a contrived, off-the-cuff example of what I'm trying to do:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class foo
  {
      int num;
  
  public:
      foo() : num(0) {}
      foo(int i) : num(i) {}
  
      explicit operator int() const { return num; }
      foo operator+(const foo&amp; rhs) const { return foo(num + rhs.num); }
      foo&amp; operator=(const foo&amp; rhs) : num(rhs.num) { return *this; };
  
      friend ostream&amp; operator&lt;&lt;(ostream&amp; o) { o &lt;&lt; num; return o; }
  };
  
  void main()
  {
      foo bar = 4; // Works fine
  
      cout &lt;&lt; bar + 3; // Error C2071.  Will be Error C2666 if 'explicit' is removed
      // cout &lt;&lt; (int) bar + 3; // If I get rid of 'explicit' above, the above
                                //   line must be switched with this to compile
  }
  </code></pre>
  
  <p>Basically, I want to be able to add or assign an <code>int</code> to a <code>foo</code> without explicit conversion of the <code>foo</code> instance.</p>
  
  <p>My first code attempt wouldn't compile, because (if I understand this correctly) when I didn't have <code>explicit</code> specified on the conversion operator, <code>bar + 3</code> counted as an operator overload -- <code>bar</code> and 3 could both be <code>foo</code> or <code>int</code>, so VS didn't know whether to run (arithmetic, arithmetic) or (<code>foo</code>, <code>foo</code>) on them.</p>
  
  <p>On my second code attempt (above), I added <code>explicit</code> to the conversion operator, so that <code>bar</code> had to be a <code>foo</code> in that situation, meaning there's no overload.</p>
  
  <p>The problem is that <a href="http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx" rel="nofollow">Visual Studio 2012 doesn't support explicit conversion operators</a>.  It gives me "Error C2071: illegal storage class" when I try to use them.</p>
  
  <p>So here's my question: is it possible to get the behavior I'm desiring?  Again - I want to be able to add or assign an <code>int</code> to a <code>foo</code> through <code>foo::operator+</code> or <code>foo::operator=</code>, without using explicit conversion on the instance of <code>foo</code>.</p>