Are there any valid use cases to use new and delete, raw pointers or c-style arrays with modern C++?

前端 未结 19 1294
梦谈多话
梦谈多话 2020-11-22 07:20

Here\'s a notable video (Stop teaching C) about that paradigm change to take in teaching the c++ language.

And an also notable blog post

19条回答
  •  情书的邮戳
    2020-11-22 07:43

    For simple use cases, smart pointers, standard containers and references should be enough to use no pointers and raw allocation and de-allocation.

    Now for the cases I can think about:

    • development of containers or other low-level concepts - after all the standard library itself is written in C++ and it does make use of raw pointers, new and delete
    • low level optimization. It should never be a first class concern, because compilers are smart enough to optimize standard code, and maintainability is normally more important than raw performance. But when profiling shows that a block of code represents more than 80% of the execution time, low level optimization makes sense, and thats one of the reasons why the low level C standard library is still a part of C++ standards

提交回复
热议问题