Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Current version:</p> <p>First of all, the <code>virtual</code> should have nothing to do with it. I'll bet that's a problem with MSVC, especially considering there's no difference on Clang. Anyway, your <code>twice</code> function is marked <code>const</code>. This means that members will be <code>const Weight</code> instead of <code>Weight</code>. This is a problem for <code>operator+</code> because it only accepts a non-const <code>this</code>. Therefore, the only way the compiler can go is to convert them to <code>double</code> and add those.</p> <p>Another problem is that adding <code>explicit</code> causes it to compile. In fact, this should <em>remove</em> the compiler's last resort of converting to <code>double</code>. This is indeed what happens on Clang:</p> <blockquote> <p>error: invalid operands to binary expression ('const Weight' and 'const Weight')<br> Weight w = weight_ + weight_;<br> note: candidate function not viable: 'this' argument has type 'const Weight', but method is not marked const</p> </blockquote> <p>Finally, making <code>operator+</code> <code>const</code> (or a free function) is the correct solution. When you do this, you might think you'll add back this route and thus have another error from ambiguity between this and the <code>double</code> route, but <code>Weight</code> to <code>const Weight &amp;</code> is a standard conversion, whereas <code>Weight</code> to <code>double</code> is a user-defined conversion, so the standard one is used and everything works.</p> <hr> <p>As of the updated code in the question, this is fine. The reason it won't compile is the fault of MSVC. For reference, it <a href="http://coliru.stacked-crooked.com/a/1ac9fd187279295d" rel="nofollow">does compile</a> on Clang. It also compiles on MSVC12 and the 2013 CTP.</p> <hr> <p>You may be storing the result in a <code>Foo</code>, but there is still an implicit conversion from <code>double</code> to <code>Foo</code> needed. You should return <code>Foo(value_ + other.value_)</code> in your addition operator so that the conversion is explicit. I recommend making the operator a free function as well because free operators are (almost) always at least as good as members. While I'm at it, a constructor initializer list would be a welcome change, too.</p> <p>In addition, from C++11 onward, a generally preferred choice is to make your conversion operator explicit:</p> <pre><code>explicit operator double() const {return value_;} </code></pre> <p>Also note the <code>const</code> added in because no state is being changed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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