Re: what is the default constructor for the POD type
On Feb 19, 7:33 am, Stanley Rice <hecong...@gmail.com> wrote:
I want to know the *default constructor* for the POD type. Since STL
implement some of the function based on the type of the paramter, fill_n
for example, may have different implementation, one for the POD type, whe=
re
no constructor will be called, and other for non-POD type. So I wrote the
code:
vector<int> vec(5);
and follow the debugger. Finlly, I come across a function:
template<typename _OutputIterator, typename _Size, typename _Tp>
inline typename
__gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::_=
_type
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
const _Tp __tmp = __value;
for (; __n > 0; --__n, ++__first)
*__first = __tmp;
return __first;
}
which is defined in stl_algobse.h. I print out the value of the parameter
*__value* and the result is:
(const int &) @0xbffff338: 0
and the value of __tmp is: 0
I didn't initialize the __value, so I think the value of variable __value
should be *random*, just like a normal variable definition without
initializaion. But, here the value is always 0, Why?
Seeing that I am the one who said to step through with debugger in
another thread, it's my fault for what you did ;-).
However, in that thread, you said that you wanted to know _how_ STL is
implemented, but here, the question is _what_ STL should be doing. And
for that, you read the documentation ;-).
Basically, when resizing (even during construction), vector default-
initializes it's elements. For "primitive" types, that means setting
to 0.
As Alf hinted, C++ can do this:
int i; // i==random memory contents. Primitive and POD types can start
their life like this, uninitialized.
and this:
int i(); // i==0. This is default initialization.
Goran.