tags:

views:

60

answers:

1

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 )

+8  A: 

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:

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

avakar
@avakar: a similar example compiled in our application, so VS, at least in theory, supports this. I was asking why it didn't work as expected.
Tom
Yes, VS2008 supports this as an extension (as does gcc, I believe). Apparently, the debugger does not handle this correctly and you can't really blame it.
avakar
A nice illustration of the general uselessness of language extensions!
anon