I have enanoutered a problem with a code similar to this
void aFuncion()
{
struct entry
{
std::string field1;
int field2;
int field3;
entry(const entry& ent)
{
// copy constructor code
}
entry()
{
// default constructor code
}
entry(std::string s, int a, int b)
{
field1 = s;
field2 = a;
field3 = b;
}
}; //end of structure definition
std::vector<entry> vec;
entry en("a string", 1, 2);
vec.push_back(en); // vec has garbage in index 0
}
after pushing the entry into the vector the debuggers shows only garbage in the vector first entry.
the problem resolved once we took the structure definition out of the function.
why did the problem occur how did getting the definition into out of the function resolved it ?
(we are working with VS 2008 on XP 32 bit )
The current C++ standard does not allow the template arguments to be locally defined types. This is remedied in the upcoming version of the standard.
14.3.1/2: