Summary
I have seen a few questions on std::vector recently, and out of curiosity I have been playing around with them a little. I've never really used the STL much, but I knew you could use vector to deal with the allocation of arrays of objects, and I could have sworn there was a way to use the default constructor to allocate the items within when the vector is created. Indeed, this question Initializing a std::vector with default constructor deals with initializing a vector using either a copy constructor and default value vs. just using the default constructor.
However, as I have been doing some experimenting in Visual Studio 2010 with a C++ Console Application project, I have not been getting results consistent with this explanation. According to one of the comments in the answer to the aforementioned question (given here), if you use, e.g., std::vector<FooClass> FooArray = new std::vector<FooClass>(20);
it should use the default constructor, and this was indeed the behavior I expected.
However I wrote some tracing code to track objects as they were created, assuming they would be created with the default constructor, and it appeared that every object was just created and subsequently immediately destroyed. Finally after much searching here, there, and everywhere, I went ahead and implemented a copy constructor that printed out information as well. What I am seeing is that if I initialize a vector of FooClass
using a default value, with, e.g., new std::vector<FooClass>(20, FooClass())
, then I get the expected result: a FooClass()
is instantiated, each of the items in the vector is initialized with the copy constructor as a copy of that object, and then the value used as the default is destroyed.
But, if I do new std::vector<FooClass>(20)
, instead of using the default constructor, it seems to be doing something that's a little (to me) bizarre. Twenty times, a temporary FooClass
object is created using the default constructor, an element of the array is constructed via the copy constructor using the temporary, and then the temporary is destroyed.
This really just doesn't make sense to me; but I wonder if perhaps I was just doing something wrong.
The Code
FooClass.h
#include <stdio.h>
class FooClass
{
public:
FooClass()
{
printf("Foo %i Created!\n", NumFoos);
myFooNumber = FooClass::NumFoos;
++FooClass::NumFoos;
myIsACopy = false;
}
FooClass(const FooClass& Another)
{
printf("Foo %i (a copy of Foo %i) Created!\n", FooClass::NumFoos, Another.myFooNumber);
myFooCopiedFrom = Another.myFooNumber;
myFooNumber = FooClass::NumFoos;
++FooClass::NumFoos;
myIsACopy = true;
}
void PrintMe()
{
if (myIsACopy)
printf("I'm Foo %i (a copy of Foo %i)!\n", myFooNumber, myFooCopiedFrom);
else
printf("I'm Foo %i!\n", myFooNumber);
}
~FooClass()
{
printf("Foo %i Deleted!\n", myFooNumber);
}
private:
int myFooCopiedFrom;
int myFooNumber;
bool myIsACopy;
private:
static int NumFoos;
};
FooClass.cpp
#include "FooClass.h"
int FooClass::NumFoos = 0;
FooVector.cpp
// FooVector.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <memory>
#include <vector>
#include "FooClass.h"
//#define USE_INITIALIZER
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef USE_INITIALIZER
std::vector<FooClass> myFooArray =
std::vector<FooClass>(5, FooClass());
#else
std::vector<FooClass> myFooArray =
std::vector<FooClass>(5);
#endif
for (int i=0; i < 5; ++i)
myFooArray[i].PrintMe();
printf("We're done!\n");
return 0;
}
Output with default initializer
Foo 0 Created! Foo 1 (a copy of Foo 0) Created! Foo 2 (a copy of Foo 0) Created! Foo 3 (a copy of Foo 0) Created! Foo 4 (a copy of Foo 0) Created! Foo 5 (a copy of Foo 0) Created! Foo 0 Deleted! I'm Foo 1 (a copy of Foo 0)! I'm Foo 2 (a copy of Foo 0)! I'm Foo 3 (a copy of Foo 0)! I'm Foo 4 (a copy of Foo 0)! I'm Foo 5 (a copy of Foo 0)! We're done!
Output with no initializer
Foo 0 Created! Foo 1 (a copy of Foo 0) Created! Foo 0 Deleted! Foo 2 Created! Foo 3 (a copy of Foo 2) Created! Foo 2 Deleted! Foo 4 Created! Foo 5 (a copy of Foo 4) Created! Foo 4 Deleted! Foo 6 Created! Foo 7 (a copy of Foo 6) Created! Foo 6 Deleted! Foo 8 Created! Foo 9 (a copy of Foo 8) Created! Foo 8 Deleted! I'm Foo 1 (a copy of Foo 0)! I'm Foo 3 (a copy of Foo 2)! I'm Foo 5 (a copy of Foo 4)! I'm Foo 7 (a copy of Foo 6)! I'm Foo 9 (a copy of Foo 8)! We're done!
The Question
So... Am I setting up my class improperly and this is the expected behavior? Is this perhaps a quirk of Microsoft's implementation of the STL?
Or is there some other explanation entirely?
Final Note
I removed the sgi specs and comments thereon, because, as James's answer pointed out, the sgi specification is not the actual specification. See, for instance, the resources for wikipedia's entry on C++ for links to working drafts of the real specs. Thanks everyone! :)