Deny std::vector from deleting its data

后端 未结 9 906
天涯浪人
天涯浪人 2021-01-17 11:03

I have the following case:

T* get_somthing(){
    std::vector vec; //T is trivally-copyable
    //fill vec
    T* temp = new T[vec.size()];
    memc         


        
9条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 11:37

    Isn't the way to go here simply to allocate the vector dynamically? That's how resources with a life time which is unrelated to scope are traditionally managed, and I don't see any reason to invent something extraordinary.

    Of course that vector should be destroyed some time later; that may make it necessary to store its address somewhere as a side effect of get_somthing(), but even then this strategy seems cleaner than any of the other ideas.

提交回复
热议问题