I expected the following program to print "11" but it actually prints "01" so it seems like the first assignment fails.
struct A
{
A(int i = 0) : i_(i) {}
int i_;
};
int main()
{
A x(1);
A y;
static_cast<A>(y) = x; // *** Fails to assign ***
std::printf("%i", y.i_);
y = x;
std::printf("%i", y.i_);
}
If I use a primitive type likeint
instead of A
then int x = 1; int y; static_cast<int>(y) = x;
does assign the value 1
to x
. Is there some way I can get it to work for custom types? I tried adding operator A() { return *this; }
to struct A
but that didn't work.
Obviously this is a stupid program but the problem arises in a template function where I have static_cast<std::remove_const<T>::type>(y) = x
and it was working fine for primitive types but just now failed for a custom type.