Зачем вызывается copy ctor после инициализации объекта?
//-std=gnu++03 -fno-elide-constructors
//-std=gnu++11 -fno-elide-constructors
#include <iostream>
class A {
int data;
public:
A (void) { std::cout << this << " A::A (void) \n"; }
A (A const & ref ) { std::cout << this << " A::A (A const &) \n"; }
~A(){ std::cout << this << " ~A::A () \n";}
class B;
friend B;
class B {
A & a;
public:
B(A & objc) : a(objc) { std::cout << this << " A::B::B (A &) \n"; }
B(B const & ref) : a(ref.a) { std::cout << this << " A::B::B (B const &) \n"; }
~B(){ std::cout << this << " ~A::B::B () \n";}
};
int main(void) {
A a;
// A::B b1 (a); // ok
A::B b2 = a; // strange
std::cout << " end " << std::endl;
return 0;
}
0xff98d148 A::A (void)
0xff98d14c A::B::B (A &)
0xff98d144 A::B::B (B const &)
0xff98d14c ~A::B::B ()
end
0xff98d144 ~A::B::B ()
0xff98d148 ~A::A ()
https://godbolt.org/z/GEjP95jnh
Почему вызывается конструктор копирования? Понятно, что оптимизация его выключит. Но в чем причина? Чем копилятору не нравится конструтор (A &) , что он затем переписывает объект заново? Или это просто потомучто?