Home > Backend Development > C++ > body text

C++ function exception safe parameter passing mechanism

王林
Release: 2024-04-19 11:06:02
Original
1170 people have browsed it

In C, function parameters are exception-safe when passed by value, because the actual parameters retain their original values; passing by reference does not guarantee exception safety, and the actual parameters may be in an undefined state. Best practices recommend using pass-by-value to pass primitive types, lightweight objects, and parameters that do not affect the function call, and using pass-by-reference to pass large objects or parameters that require function modification.

C++ 函数异常安全参数传递机制

C function exception safe parameter passing mechanism

In C, when calling a function, parameters can be passed by value or by Pass by reference. Passing by value means creating a copy of the actual parameter, while passing by reference uses the address of the actual parameter.

Pass by value

void foo(int x) {
  // 对 x 执行操作
}
Copy after login

Pass by reference

void foo(int& x) {
  // 对 x 执行操作
}
Copy after login

When a function throws an exception, the parameter passing mechanism has a real impact parameter status.

Pass-by-value exception safety

Pass-by-value is exception-safe because if the function throws an exception, the actual parameters will retain their original values. For example:

int main() {
  int x = 1;
  try {
    foo(x);
  }
  catch(...) {
    // x 仍为 1
  }
}
Copy after login

Pass-by-reference exception safety

Pass-by-reference does not guarantee exception safety. If a function throws an exception, the actual parameters may be in an undefined state because the function may have changed the reference. For example:

int main() {
  int x = 1;
  try {
    foo(x);
  }
  catch(...) {
    // x 可能不是 1
  }
}
Copy after login

Best Practices

To ensure exception safety, it is recommended to use pass-by-value in the following situations:

  • Parameters Is a basic type (for example, int, char, double).
  • The parameters are lightweight objects (for example, small structures).
  • Changes to actual parameters should not affect the calling function.

Use pass-by-reference when:

  • The parameter is a large object (e.g., container, complex structure).
  • Changes to actual parameters need to be reflected in the calling function.

Practical Case

Consider a function that reads a stream and writes it to a file.

Pass the stream by value:

void writeToFile(std::istream& stream, std::ofstream& file) {
  std::string line;
  while (getline(stream, line)) {
    file << line << std::endl;
  }
}
Copy after login

Pass the stream by reference:

void writeToFile(std::istream& stream, std::ofstream& file) {
  std::string line;
  while (getline(stream, line)) {
    file << line << std::endl;
  }
  stream.close(); // 按引用传递允许在函数退出时关闭流
}
Copy after login

By passing the stream by reference, we can ensure that the stream is always closed when the function exits, thus ensuring exceptions Occurs when the stream is in a closed state.

The above is the detailed content of C++ function exception safe parameter passing mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!