-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New rule proposal: exception types should be nothrow copyable #1921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Would such a rule suggest using reference counting, internally, when the exception object contains dynamically allocated memory? It's quite common for a user-defined exception class to have a dynamically allocated string, holding an error message. |
Yes, a
|
Editors call: We think this is a good suggestion. We plan to extend E.16 to include exception type copy/move construction. |
@hsutter Thank you for addressing this issue, cool! Small nitpick, it now says at CppCoreGuidelines/CppCoreGuidelines.md Line 16137 in d5907d6
Sounds a bit interesting. Of course, I guess you mean something like: even when throwing an Right? Would you like to fix it directly on the master branch, or would you rather have a pull request? |
* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922) The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters. With help from Sergey Zubkov. * SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920) Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild. With help from Sergey Zubkov. * Extended E.16 to include copy ctor for exception type, closes isocpp#1921 * Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925) * restored reference * Added references to note Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922) The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters. With help from Sergey Zubkov. * SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920) Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild. With help from Sergey Zubkov. * Extended E.16 to include copy ctor for exception type, closes isocpp#1921 * Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925) * C.166: Use markdown format and document title for link (isocpp#1929) * C.9: Improve an inline link (isocpp#1933) Avoid ending the sentence with a dangling "see". Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]> Co-authored-by: Francisco Moretti <[email protected]> Co-authored-by: Sven van Haastregt <[email protected]>
* Update CppCoreGuidelines.md * Update CppCoreGuidelines.md * Update isocpp.dic * use snake casing * sake case naming * C 32 comments (#3) * F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922) The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters. With help from Sergey Zubkov. * SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920) Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild. With help from Sergey Zubkov. * Extended E.16 to include copy ctor for exception type, closes isocpp#1921 * Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925) * restored reference * Added references to note Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]> Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922) The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters. With help from Sergey Zubkov. * SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920) Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild. With help from Sergey Zubkov. * Extended E.16 to include copy ctor for exception type, closes isocpp#1921 * Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925) * C.166: Use markdown format and document title for link (isocpp#1929) * C.9: Improve an inline link (isocpp#1933) Avoid ending the sentence with a dangling "see". * C 32 (#5) * Update CppCoreGuidelines.md * Update CppCoreGuidelines.md * Update isocpp.dic * use snake casing * sake case naming * C 32 comments (#3) * F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922) The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters. With help from Sergey Zubkov. * SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920) Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild. With help from Sergey Zubkov. * Extended E.16 to include copy ctor for exception type, closes isocpp#1921 * Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925) * restored reference * Added references to note Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]> Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]> Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]> Co-authored-by: Francisco Moretti <[email protected]> Co-authored-by: Sven van Haastregt <[email protected]>
* Update CppCoreGuidelines.md * Update CppCoreGuidelines.md * Update isocpp.dic * use snake casing * sake case naming * C 32 comments (#3) * F.16 ("in" parameters): Move Matrix example to F.20 (return values) (#1922) The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters. With help from Sergey Zubkov. * SL.io.50 (Avoid `endl`): Mention string streams (#1920) Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild. With help from Sergey Zubkov. * Extended E.16 to include copy ctor for exception type, closes #1921 * Fix GitHub Actions build warnings, Marker style should be `*` (#1925) * restored reference * Added references to note Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]> Co-authored-by: Niels Dekker <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
There is a similar rule in the SEI CERT C++ Coding Standard: ERR60-CPP. Exception objects must be nothrow copy constructible (down at the time of submitting this issue).
Rationale: exception objects are copy-initialized, and if an implementation chooses to not elide this copy, or there is an additional copy at the catch site (i.e. not following E.15: Throw by value, catch exceptions from a hierarchy by reference), and the copy constructor throws, then
std::terminate
is called.It's sufficient if the exception type is nothrow copy constructible, but I can't see why someone would want to make a throwing copy assignment operator at the same time, aside from doing some magic in the copy constructor and not bothering to define a copy assignment operator, but that already violates the rule of three / five.
The text was updated successfully, but these errors were encountered: