2

Why does the following code

#include <iostream>

struct A {
    template<typename T>
    A &operator=(T &&rhs) {
        std::cout << "A::operator= called" << std::endl;
        return *this;
    }
};

int main() {
    A a1;
    a1 = A();
    return 0;
}

print A::operator= called using Visual Studio Express 2013 but prints nothing when compiled with gcc-4.9.1.

What would be the correct behavior?

Edit: Template assignment operator overloading mystery does not address VS/gcc compiler differences.

Community
  • 1
  • 1
Florian
  • 1,036
  • 10
  • 15
  • possible duplicate of [Template assignment operator overloading mystery](http://stackoverflow.com/questions/5625790/template-assignment-operator-overloading-mystery) – user657267 Mar 11 '15 at 12:16
  • 2
    This is a [known bug](http://stackoverflow.com/questions/8991874/why-is-this-code-trying-to-call-the-copy-constructor) in Visual Studio. It doesn't implicitly generate move-constructor and move-assignment-operator. – M.M Mar 11 '15 at 12:47

1 Answers1

2

GCC is correct. Your type has an implicitly declared move assignment operator, which is a better match than the template.

If you cause the implicit move assignment to be suppressed, e.g. by adding a user-declared destructor, then your template will be used.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521