Can I leave dynamic memory for std::complex uninitialized after allocation?

90 Views Asked by At

I have a container library that initializes elements only if they are not "is_trivially_default_constructible". In principle, I use the std::is_trivially_default_constructible trait to determine this.

I think this should work if the trait reflects reality, for example ints and doubles are left uninitialized in effect.

1. Is it ok to do that, should I expect some undefined behavior? (Assume that elements are eventually formed by assignment.)

There are plenty examples like that where it is suggested that one uses a special allocator in which the construct function is a no-op.

If the answer above is positive, what can it be said about the following more complicated case:

In turn, I also use the mechanism and "overwrite" the behavior for std::complex<double>. That is I treat std::complex<double> as if is_trivially_default_constructible were true. (It is not true, because the default constructor of std::complex is stupid.)

2. Is it ok if I don't initialize the memory for a type that technically is not trivial to default-construct? Should I "bless" somehow this memory with std::launder or something like that?

0

There are 0 best solutions below