Why can't I wrap a T* in an std::vector?

后端 未结 3 2088
清歌不尽
清歌不尽 2020-12-11 21:55

I have a T* addressing a buffer with len elements of type T. I need this data in the form of an std::vector, for

3条回答
  •  半阙折子戏
    2020-12-11 22:26

    These days - you no longer need to wrap a T* in an std::vector, you can wrap it with an std::span (in C++20; before that - use gsl::span). A span offers you all the convenience of a standard library container - in fact, basically all relevant features of std::vector excluding changes to the size - with a very thin wrapper class. That's what you want to use, really.

    For more on spans, read: What is a "span" and when should I use one?

提交回复
热议问题