C++ noexcept Keyword
The noexcept
keyword in C++ is used to indicate that a function does not throw exceptions. It serves as a guarantee to the compiler and the programmer that the function will not propagate exceptions.
The noexcept
specifier can be applied to functions, lambdas, and operator overloads. It is particularly useful in performance-critical code, where knowing that a function cannot throw exceptions enables the compiler to optimize better.
Syntax
</>
Copy
return_type function_name(parameters) noexcept {
// Function body
}
- return_type
- The type of value returned by the function.
- function_name
- The name of the function to which the
noexcept
specifier is applied. - parameters
- The input parameters for the function.
- noexcept
- A specifier that guarantees the function does not throw exceptions.
Examples
Example 1: Basic Usage of noexcept
In this example, you will learn how to declare a function with the noexcept
specifier.
</>
Copy
#include <iostream>
using namespace std;
void safeFunction() noexcept {
cout << "This function does not throw exceptions." << endl;
}
int main() {
safeFunction();
return 0;
}
Output:
This function does not throw exceptions.
Explanation:
- The function
safeFunction
is declared with thenoexcept
specifier, guaranteeing that it does not throw exceptions. - When the function is called in
main
, it executes normally without throwing any exceptions.
Example 2: Conditional noexcept
In this example, you will learn how to use noexcept
conditionally based on a compile-time expression.
</>
Copy
#include <iostream>
using namespace std;
void mayThrow(bool throwException) {
if (throwException) {
throw runtime_error("An exception was thrown!");
}
}
void safeFunction(bool condition) noexcept(noexcept(mayThrow(condition))) {
if (condition) {
mayThrow(false); // This will not throw
}
}
int main() {
safeFunction(true);
cout << "Execution continued normally." << endl;
return 0;
}
Output:
Execution continued normally.
Explanation:
- The
safeFunction
uses conditionalnoexcept
based on whethermayThrow
can throw exceptions. - The
noexcept
operator checks whether the function argument isnoexcept
at compile time. - Since
mayThrow
does not throw exceptions in this case, the program executes normally.
Example 3: Lambdas with noexcept
In this example, you will learn how to declare a noexcept
lambda.
</>
Copy
#include <iostream>
using namespace std;
int main() {
auto safeLambda = []() noexcept {
cout << "This is a noexcept lambda function." << endl;
};
safeLambda();
return 0;
}
Output:
This is a noexcept lambda function.
Explanation:
- The lambda function is declared with the
noexcept
specifier, ensuring it does not throw exceptions. - When the lambda is invoked, it executes normally without throwing any exceptions.
Key Points about noexcept
Keyword
- The
noexcept
specifier guarantees that a function does not throw exceptions. - Using
noexcept
can enable better compiler optimizations and improve performance. - You can use conditional
noexcept
to determine the specifier based on compile-time conditions. - The
noexcept
operator evaluates totrue
orfalse
depending on whether a given function isnoexcept
.