I have a C++ class that has the following interface:
class F {
public:
F(int n, int d);
// no other constructors/assignment constructors defined
F& operator *= (const F&);
F& operator *= (int);
int n() const;
int d() const;
};
And I have the following code:
const F a{3, 7};
const F b{5, 10};
auto result = F{a} *= b; // How does this compile?
Under Visual Studio (VS) 2013, the commented line compiles without error. Under VS2015 , error C2678 is produced:
error C2678: binary '*=': no operator found
which takes a left-hand operand of type 'const F'
(or there is no acceptable conversion)
note: could be 'F &F::operator *=(const F &)'
note: or 'F &F::operator *=(int)'
note: while trying to match the argument list '(const F, const F)'
My expectation was that F{a}
would create a non-const temporary copy of a
to which operator *= (b)
would be applied, after which the temporary object would be assigned to result
. I did not expect the temporary to be a constant. Interestingly: auto result = F(a) *= b;
compiles without error in VS2015, which I thought should be semantically the same.
My question is: which behaviour is correct VS2015 or VS2013 & why?
Many thanks