I know STL containers like vector copies the object when it is added. push_back method looks like:

void push_back ( const T& x );

I am surprised to see that it takes the item as reference. I wrote a sample program to see how it works.

struct Foo
{
    Foo()
    {
        std::cout << "Inside Foo constructor" << std::endl;
    }

    Foo(const Foo& f)
    {
        std::cout << "inside copy constructor" << std::endl;
    }
};

Foo f;
std::vector<Foo> foos;
foos.push_back(f);

This copies the object and I can see it is calling copy-constructor.

My question is, when the push_back takes item as reference, how it is calling copy-constructor? Or am I missing something here?

Any thoughts..?

Edit
Report