Home > Backend Development > C++ > Should We Still Pass std::string by const &?

Should We Still Pass std::string by const &?

DDD
Release: 2024-12-12 14:27:14
Original
927 people have browsed it

Should We Still Pass std::string by const &?

The Bygone Era of Const Std::string & Parameters

In a recent discussion, Herb Sutter raised the notion that the prevailing practice of passing std::vector and std::string by const & parameter may no longer hold the same weight. His proposed alternative, passing these objects by value, has sparked debate and raised questions.

Understanding the Transition

Why did Sutter advocate this shift? The primary reason lies in scenarios similar to the one described below:

Consider a function A that calls function B, which in turn calls function C. Function A passes a string through B and into C, maintaining its unawareness of C's existence.

The Const & Approach:

When B and C receive the string as const & parameters, the interactions occur as follows:

void B(const std::string &str) {
  C(str);
}

void C(const std::string &str) {
  // Manipulates `str` but does not store it.
}
Copy after login

This approach effectively circulates pointers, avoiding unnecessary copying or movement. C accepts const & because it only intends to use the string, not store it.

The Value Approach:

However, if C's functionality requires it to store the string, the following modification is made:

void C(const std::string &str) {
  // Manipulates `str`.
  m_str = str;
}
Copy after login

As you can see, C no longer simply uses the string but also stores it. C 11's move semantics should theoretically eliminate unnecessary copying, but C's const & parameter prevents this. The copy constructor and potential memory allocation come into play instead.

Bypassing Const &:

Bypassing the const & parameter and passing the string by value through all functions eliminates this issue. std::move can efficiently shuffle the data where needed. If a function intends to hold onto the string, it can do so.

Performance Considerations:

Passing by value does incur a higher performance expense compared to using references. However, for small strings benefiting from SSO, the cost is negligible.

Conclusion

The decision of whether to pass std::string by const & or value hinges upon the specific use case and its sensitivity to memory allocations. While passing by value may not be universally preferable, it offers compelling advantages in certain situations.

The above is the detailed content of Should We Still Pass std::string by const &?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template