When compiled with gcc 4.7.2 or 4.8.1 and run the following program
#include <stdio.h>
#include <functional>
class A
{
public:
A()
{
}
A(const A& a)
{
printf("Copy ctor\n");
}
A(A&& a)
{
printf("Move ctor\n");
}
};
int main()
{
A a;
auto func = [a] {
};
auto newfunc = std::move(func);
return 0;
}
will give the output:
Copy ctor
Move ctor
which seems to be perfectly normal.
However, when A a;
is changed to const A a;
, the output is as follows:
Copy ctor
Copy ctor
Why is the move of lambda affected by the fact whether the original variable has been const or not?
FWIW, MSVC2012 always makes two copies.