Are the days of passing const std::string & as a parameter over?

后端 未结 13 1942

I heard a recent talk by Herb Sutter who suggested that the reasons to pass std::vector and std::string by const & are largely gon

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 17:29

    IMO using the C++ reference for std::string is a quick and short local optimization, while using passing by value could be (or not) a better global optimization.

    So the answer is: it depends on circumstances:

    1. If you write all the code from the outside to the inside functions, you know what the code does, you can use the reference const std::string &.
    2. If you write the library code or use heavily library code where strings are passed, you likely gain more in global sense by trusting std::string copy constructor behavior.

提交回复
热议问题