C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A type trait to detect if value initialization can be achieved by zero-filling

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Mon, 30 Jan 2023 15:24:49 +0100
Il 30/01/23 12:56, Ville Voutilainen via Std-Proposals ha scritto:
> This proposal is for a facility that hooks into implementation-defined
> properties in order
> to allow writing optimized implementations based on them. If your
> foremost concern is portability
> in the sense of your code having the exact same semantics on every
> implementation, don't use it.

To be honest I don't understand the portability concerns. Could you
please elaborate a little?

Yes, the value of is_trivially_value_initializable_by_zero_filling_v<X>
for a given X may change depending on the platform. You can certainly
static_assert on it if you're depending on a specific value; but more
realistically, I am expecting that users of the type will use it as an
trait to apply an optimization conditionally, for instance like this:


template <class T>
void MyVector::uninitialized_value_construct(std::span<T> s) {
   if (s.empty()) return; // don't pass nullptr to memset

   if constexpr (std::itvibzf_v<T> || CUSTOM_OPT_IN_TRAIT<T>)
     std::memset(s.data(), 0, s.size_bytes());
   else
     std::uninitialized_value_construct(s.begin(), s.end())

}


The result is portable and will do the right thing on each platform. Or
was it some other concern?

Thanks,
-- 
Giuseppe D'Angelo

Received on 2023-01-30 14:24:52