I came across code that passes a lambda as an argument to emplace_back, which confused me. So I wrote a small test to validate the syntax like this:
struct A
{
    int a;
    A(){cout<<"constructed"<<endl;}
    A(const A& other){a = other.a; cout<<"copied"<<endl;}
    A(const A&& other){a = other.a; cout<<"moved"<<endl;}
};
int main()
{
   vector<A> vec;
   vec.emplace_back(
       []{A a;
       a.a = 1;
       return a;   
       }());
   A a2;
   a2.a = 2;
   vec.emplace_back(std::move(a2));
  return 0;
}
I have two questions: 1) Can someone clarify how a lambda can be passed as an argument to emplace_back? I had only seen constructor arguments being passed to emplace back in the past. 2) The output was:
constructed
moved
constructed
moved
copied
Where is the last copied coming from? Why aren't the two approaches equivalent.
 
     
    