Derived Assignment Operator and Perfect Forwarding

Consider this minimal example:

include <iostream>

struct foo
{
      foo &operator=(const foo &)
      {
              std::cout << "base copy assignment\n";
              return *this;
      }
      foo &operator=(foo &&)
      {
              std::cout << "base move assignment\n";
              return *this;
      }
};

struct bar: foo
{
      template <typename T>
      bar &operator=(T &&x)
      {
              std::cout << "derived generic assignment\n";
              foo::operator=(std::forward<T>(x));
              return *this;
      }
};

int main()
{
    bar b, c;
    b = c;
    b = bar{};
}

The output of this program:

derived generic assignment
base copy assignment
base move assignment

whereas I expect it to be:

derived generic assignment
base copy assignment
derived generic assignment
base move assignment

Or, in other words, it seems that the ideal forwarding of the = () operator in the bar does not kick in the case of a destination assignment: instead, the destination main destination operator is called.

I tried the same test with a simple member function, and in this case the result will be what I would expect (i.e. a perfectly redirected assignment in a bar is always called before any of the basic assignments). Also, with GCC 4.5, unlike the pre-release 4.6, which I am currently using, the behavior is the same as expected.

Is this specific behavior for assignment statements, or is this a recent GCC bug?

+3
1

. , , , . , . , .

, , c const.

:

, , , , . . [class.copy] .

, X :

operator=(X&&);
operator=(const X&&);
operator=(volatile X&&);
operator=(const volatile X&&);

( ).

. .

+2

All Articles