views:

96

answers:

3
+12  A: 

Your variable needs a name:

static std::vector<int> my_static_vector(3);
James McNellis
Why did this get downvoted instantaneously?
Michael Mrozek
@Michael: Good question. Must find whoever keeps downvoting without leaving a freaking COMMENT! That is what they are for! (Oh, and +1)
Billy ONeal
@James Do you know by the chance, why does this happen? Why can't an unnamed object, which does some work in the constructor be a "normal" static variable and don't get constructed-destructed?
HardCoder1986
@HardCoder1986: A variable must have a name. The code in your original question is not valid C++ (though at least one compiler accepts it without issuing a warning). Unnamed objects are temporaries and follow special lifetime rules (basically, they exist only until the end of the full expression in which they were created).
James McNellis
@Michael: If it was downvoted, the downvote was undone. Maybe it was just a mistake.
James McNellis
On a related note, that compiler vendor does not intend to fix that bug at this time. See https://connect.microsoft.com/VisualStudio/feedback/details/582129.
James McNellis
+7  A: 

You forgot to give the vector a name, so without any variable pointing to it it's destroyed immediately after it's created

Michael Mrozek
+1 for *also* being a correct answer.
Billy ONeal
+4  A: 

Because std::vector<int>(3) creates an unnamed temporary, which lives only to the end of it's contained expression. The debugger can't show destruction in the same line as construction though, so it shows it on the next line.

Give the item an name and normal static semantics will apply.

Billy ONeal
Lol -- don't everyone pile on at once!
Billy ONeal