This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is avoding calling default constructor possible?
- From: brac37 <MichieldeB at aim dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 4 Sep 2010 11:41:35 -0700 (PDT)
- Subject: Re: Is avoding calling default constructor possible?
- References: <c55009e70911011913k4889a783ke464f56142560d6e@mail.gmail.com>
Åukasz Lew wrote:
>
> Hi,
>
> Is it possible to create array of objects T without calling the
> default constructors T::T() ?
> Calling other constructor instead would be great.
> Leaving the memory uninitalize is ok as well, as I can do placement new
> later.
>
> I want to avoid char* casting to T* because it breaks strict aliasing
> rules
> (stopping optimizations from happening and slowing my program by 25%.)
>
> Answer to this question would provide walkaround to the aliasing
> problem I described few days ago.
>
> Thanks in advance for any ideas
> Lukasz Lew
>
>
> PS
> Manual says that uinon is the way to go with strict-aliasing, but
> unfortunately:
>
> union {
> char tab [N * sizeof(Elt)];
> Elt tab2 [N];
> };
>
> Doesn't compile because Elt *has* default constructor.
>
>
Yes, you can do
> blah = new foo(a,b);
and
> blah = new foo[c];
but not
> blah = new foo(a,b)[c];
How stupid. Though you can get the same effect by
> foo bar(a,b);
> blah = malloc(c * sizeof(foo));
> for (int k=0; k<c; k++) blah[k] = foo;
where the copy assignment operator is the one made by the compiler, which
copies data members with their own copy assignment operators.
--
View this message in context: http://old.nabble.com/Is-avoding-calling-default-constructor-possible--tp26157694p29621728.html
Sent from the gcc - Help mailing list archive at Nabble.com.