Is avoding calling default constructor possible?

View: New views
3 Messages — Rating Filter:   Alert me  

Is avoding calling default constructor possible?

by Łukasz Lew :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Re: Is avoding calling default constructor possible?

by Andrew Haley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

?ukasz Lew wrote:

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

I don't really understand why you need this.  character types are special
in that they can alias all POD types.  Is the problem here that you're
trying to build a backing array of char type for a non-POD object?

Andrew.


3.9 Types

For any complete POD object type T, whether or not the object holds a
valid value of type T, the underlying bytes (1.7) making up the object
can be copied into an array of char or unsigned char. 36) If the
content of the array of char or unsigned char is copied back into the
object, the object shall subsequently hold its original value.

[Example:
#define N sizeof(T)
char buf[N];
T obj;                                 // obj initialized to its original value
memcpy(buf, &obj, N);                  // between these two calls to memcpy,
                                        // obj might be modified
memcpy(&obj, buf, N);                  // at this point, each subobject of obj of scalar type
                                        // holds its original value

36) By using, for example, the library functions (17.4.1.2) memcpy or
memmove.

Re: Is avoding calling default constructor possible?

by graphicsMan69 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It is easy to create an array of objects T that are uninitialized.
malloc would certainly work.

On Mon, Nov 2, 2009 at 2:18 AM, Andrew Haley <aph@...> wrote:

> ?ukasz Lew wrote:
>
>> 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.
>
> I don't really understand why you need this.  character types are special
> in that they can alias all POD types.  Is the problem here that you're
> trying to build a backing array of char type for a non-POD object?
>
> Andrew.
>
>
> 3.9 Types
>
> For any complete POD object type T, whether or not the object holds a
> valid value of type T, the underlying bytes (1.7) making up the object
> can be copied into an array of char or unsigned char. 36) If the
> content of the array of char or unsigned char is copied back into the
> object, the object shall subsequently hold its original value.
>
> [Example:
> #define N sizeof(T)
> char buf[N];
> T obj;                                 // obj initialized to its original
> value
> memcpy(buf, &obj, N);                  // between these two calls to memcpy,
>                                       // obj might be modified
> memcpy(&obj, buf, N);                  // at this point, each subobject of
> obj of scalar type
>                                       // holds its original value
>
> 36) By using, for example, the library functions (17.4.1.2) memcpy or
> memmove.
>