Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14263 > unrolled thread

what is the default constructor for the POD type

Started byStanley Rice <heconghui@gmail.com>
First post2012-02-18 22:33 -0800
Last post2012-02-20 00:21 +0100
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  what is the default constructor for the POD type Stanley Rice <heconghui@gmail.com> - 2012-02-18 22:33 -0800
    Re: what is the default constructor for the POD type "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-02-19 07:48 +0100
    Re: what is the default constructor for the POD type Goran <goran.pusic@gmail.com> - 2012-02-19 09:20 -0800
      Re: what is the default constructor for the POD type Melissa <melis@a.com> - 2012-02-19 18:44 +0100
      Re: what is the default constructor for the POD type "Asger Joergensen" <Junk@Asger-P.dk> - 2012-02-19 18:29 +0000
      Re: what is the default constructor for the POD type "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-02-20 00:21 +0100

#14263 — what is the default constructor for the POD type

FromStanley Rice <heconghui@gmail.com>
Date2012-02-18 22:33 -0800
Subjectwhat is the default constructor for the POD type
Message-ID<380648.381.1329633206448.JavaMail.geo-discussion-forums@pbeo1>
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, where 
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?

Thanks.

[toc] | [next] | [standalone]


#14264

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2012-02-19 07:48 +0100
Message-ID<jhq604$ibc$1@dont-email.me>
In reply to#14263
On 19.02.2012 07:33, Stanley Rice 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, where
> 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?
>
> Thanks.

`std::vector` guarantees to initialize its elements.

If you don't provide an initial value then the default one for type T is 
T().

And writing int() is the same as writing 0. It is an example of *pseudo 
constructor* notation. As I recall, the standard does not define or use 
that term, but does talk about "pseudo destructor" for the corresponding 
notation for destruction (which you can't apply directly to an int).

So, it's not compiler specific behavior that you're seeing.

The initialization is guaranteed by std::vector, and the particular 
value for the int type, namely 0, is specified elsewhere in the standard.


Cheers & hth.,

- Alf

[toc] | [prev] | [next] | [standalone]


#14291

FromGoran <goran.pusic@gmail.com>
Date2012-02-19 09:20 -0800
Message-ID<42d7bfa4-2beb-4e44-b819-8f7d4efa607c@h6g2000yqk.googlegroups.com>
In reply to#14263
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, where
> 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.

[toc] | [prev] | [next] | [standalone]


#14292

FromMelissa <melis@a.com>
Date2012-02-19 18:44 +0100
Message-ID<jhrceb$69h$1@news.albasani.net>
In reply to#14291
On Sun, 19 Feb 2012 09:20:20 -0800 (PST)
Goran <goran.pusic@gmail.com> wrote:

> 
> int i(); // i==0. This is default initialization.

It looks like function declaration...

[toc] | [prev] | [next] | [standalone]


#14293

From"Asger Joergensen" <Junk@Asger-P.dk>
Date2012-02-19 18:29 +0000
Message-ID<xn0hujgdpqd8qkq00f@news.astraweb.com>
In reply to#14291
Hi Goran

Goran wrote:

> 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.

Not true, that is a function declaration not an initialization.

int i = 0; 
will initialize i to 0

and if You have more then on:

int i[10] = {0};
now they are all initialized to 0

Best regards
Asger-P

[toc] | [prev] | [next] | [standalone]


#14300

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2012-02-20 00:21 +0100
Message-ID<jhs05n$uo2$1@dont-email.me>
In reply to#14291
On 19.02.2012 18:20, Goran wrote:
>
> 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.

Well, it would be, except that that latter is a function declaration.

It's "the most vexing parse" of C++.

However,

   int i = int();

initializes i with the int default value.

Which is not quite the same as the /formal/ term "default 
initialization" means, but near enough: it's initialization, and it's 
the default value. :-)


Cheers & hth.,

- Alf

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web