Benefits of using reference_wrapper instead of raw pointer in containers?

前端 未结 2 580
南旧
南旧 2021-02-02 10:01

What benefits has using std::reference_wrapper as template parameter of containers instead of raw pointers? That is std::vector

2条回答
  •  有刺的猬
    2021-02-02 10:55

    I don't think there is any technical difference. Reference wrapper provides basic pointer functionality, including the ability to change the target dynamically.

    One benefit is that it demonstrates intent. It tells people who read the code that "whoever" has the variable, isn't actually controlling its lifespan. The user hasn't forgotten to delete or new anything, which some people may start to look for when they see pointer semantics.

提交回复
热议问题