1

Given a pointer to an array of N elements of char, I want to create an object which will encapsulate the array (it's address, length and type) giving me a read-only interface to it.

I have been using std::vector but the constructor makes a copy of the original array. I want a solution that avoids the copy.

This is very similar to this existing question: STL non-copying wrapper around an existing array? but I'm trying to avoid maintaining the size and pointer separately and instead have it encapsulated in a single object using a standard library implementation.

Edit: my library processes buffers asynchronously after adding them to a queue. Up until now I have queued the input buffer after copying it into an std::vector. I have just confirmed that the caller will not change the input array till the processing has completed. So, I want to replace the std::vector copy with an object that wraps the original input.

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • 7
    [`std::span`](https://en.cppreference.com/w/cpp/container/span) – Konrad Rudolph Aug 24 '23 at 12:06
  • 1
    You will need to write a class that implements all requirements of a container, provide its own iterators, etc. How to do this is the same way anything else is done in C++: write the code, test it, see if it works. Your question is unclear. Are you asking if there's a template in the C++ library that will do this for you? There might be something in the ranges library, in the current C++ standard, that will provide a range to play with. – Sam Varshavchik Aug 24 '23 at 12:06
  • 2
    Or boost::span if C++20 is not yet an option – Botje Aug 24 '23 at 12:08
  • 1
    What C++ standard? Is `N` a compile-time constant, or is it unknown (and read or calculated) at run time? What is the lifetime of the array you're given a pointer to (e.g. can you assume it still exists for as long as the object that provides a read-only interface to it?)? – Peter Aug 24 '23 at 12:15
  • 1
    "Are you asking if there's a template in the C++ library that will do this for you?" That's exactly what I'm asking at the tail end of the post. – Agnel Kurian Aug 24 '23 at 12:15
  • 1
    Where and how do you want to use this wrapper, in the end it is most likely to end up being a view (which will be invalidated after adding/removing elements from the vector). Or in other words what kind of behavior and lifetimes do you expect? (up-to and including thread safety) – Pepijn Kramer Aug 24 '23 at 12:16
  • @PepijnKramer my library receives a pointer to an array and size as input. The caller will not change the array during the lifetime of my object. I just want to encapsulate the array in a safe/convenient wrapper. – Agnel Kurian Aug 24 '23 at 12:20
  • @AgnelKurian, write a sample code that shows the intended usage. Just assume that the thing you look for exists and write code that would use it. – Enlico Aug 24 '23 at 12:21
  • `std::string_view(reinterpret_cast(pointer_to_first_element_in_orig_array), size_in_bytes);` - or if you'd like an interface that knows about the real type too, you could use this [`template struct char_view;`](https://stackoverflow.com/a/62505567/7582247) – Ted Lyngmo Aug 24 '23 at 12:41
  • `std::vector>` would be one option. Add `const` as needed. – Jesper Juhl Aug 24 '23 at 13:22

0 Answers0