Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2005-05-27 14:37:04


On May 27, 2005, at 1:47 PM, Peter Dimov wrote:

> Howard Hinnant wrote:
>
>> 2. Uninitialized is better because it is much faster, especially if
>> you have containers of such elements. You can always explicitly
>> initialize if you want.
>
> Technically... you can't put an uninitialized (non-character) scalar
> in a container; copying an uninitialized scalar is undefined behavior.

<gets an evil look in his eye...>

#include <array>
#include <vector>
#include <tupleio>
#include <iterator>
#include <algorithm>
#include <iostream>

int main()
{
     std::vector<std::tr1::array<int, 3> > v;
     std::tr1::array<int, 3> a;
     for (int i = 0; i < 10; ++i)
         v.push_back(a);
     std::ostream_iterator<std::tr1::array<int, 3> > out(std::cout,
"\n");
     copy(v.begin(), v.end(), out);
}

(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)
(1 0 3195936)

Ha, ha, did it! And you can't stop me from doing it again! <evil laugh
follows> :-)

Whatta ya gonna do, send the UBP squad (Uninitialized Behavior Police)
over to my house? I'm setting the barricades up now. I've got 3 dogs
I can hold hostage too! (well, 2.5 dogs really, one of them is a
Maltese - http://www.malteseonly.com/ - I'll release the little yapper
first) :-)

Ok, kidding aside, yes, you've got technical on your side. But I've
got practicality and performance on mine. On most machines and for
most scalars, you can copy uninitialized to your heart's content. And
when you can't, initialization is both easy and readable. I find
forced initialization for everyone everywhere just a little too
Java-ish.

Next thing you know someone is going to want to take away my precious
goto. :-)

-Howard

PS: Whoa, it must be Friday afternoon! :-)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk