Is avoding calling default constructor possible?

brac37 MichieldeB@aim.com
Sat Sep 4 18:41:00 GMT 2010




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



More information about the Gcc-help mailing list