Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no std::data() overload for std::valarray?

C++11 introduced std::begin(std::valarray&) as well as std::end(std::valarray&).

C++17 introduced std::data() which works with std::vector, std::array, C-style arrays, etc. But why wasn't an overloaded std::data() introduced for std::valarray?

std::valarray is specified to have contiguous storage, which can be accessed by taking the address of a[0] (see Notes).

std::data(std::valarray& a) could have simply been defined to return &(a[0]). Why hasn't this been done? Is it an oversight?

My motivation is that I'm working on a general-purpose serialization library. When it receives contiguous binary number arrays from a source (such as CBOR), it detects if the destination container has an overloaded data(container) function, a container.resize(n) member function, as well as an appropriate value_type (matching primitive number type). The existence of all three makes it possible to efficiently memcpy() the source data directly into the destination container. It would make my life simpler if there was a std::data(std::valarray&) overload. The lack of it isn't a showstopper, but it does make the code more messy.


ADDENDUM: The reason why I want to detect a data function is that it tells me that the destination container is contiguous. If it's contiguous, then I can do an efficient byte copy (via std::memcpy or std::copy doesn't really matter). If it's not contiguous, then I have to unpack each unaligned source array number one at a time and append it to the destination container using push_back, emplace, etc depending on the container type.


ADDENDUM 2: I've decided to use an adaptor and traits approach instead of detecting the presence of a data function. This will make it easier to support non-standard or user-defined container types. My question about why there is no std::data(std::valarray& a) still stands.


ADDENDUM 3: I should have clarified that I need to do this hackery for CBOR typed arrays, which can only be numbers. Furthermore, the numbers in the source buffer are not aligned to element boundaries. I'm aware that the binary data may need endian swapping, and that copying bytes to a floating point type may trigger weird NaN behavior if not treated carefully.

I now regret mentioning my motivation, and should have let the std::data(std::valarray& a) question stand on its own. What a trainwreck this question has become, haha. :-)

like image 413
Emile Cormier Avatar asked Feb 06 '21 00:02

Emile Cormier


1 Answers

As 1201ProgramAlarm stated in the comments, the proposal to add std::data does not make any mention of std::valarray. Unless someone can point out why &(a[0]) can't be used to obtain the valarray's data pointer, the simple answer is that valarray was either forgotten or ignored in the proposal.

like image 51
Emile Cormier Avatar answered Sep 22 '22 13:09

Emile Cormier