Skip to content
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

[unique.ptr.single] Throwing after 079f7d3b02d can be well-defined #4872

Closed
JohelEGP opened this issue Sep 6, 2021 · 4 comments
Closed

[unique.ptr.single] Throwing after 079f7d3b02d can be well-defined #4872

JohelEGP opened this issue Sep 6, 2021 · 4 comments

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented Sep 6, 2021

079f7d3 added noexcept to certain constructors that had the precondition that some initialization didn't throw. Before, the effects was that throwing results in undefined behavior according to [structure.specifications]. 079f7d3 didn't remove the preconditions, but an exception throw within a noexcept function is well-defined. Could this have been an oversight?

@jensmaurer
Copy link
Member

No, throwing an exception in these situation is still a precondition violation -> undefined behavior. The unconditional "noexcept" just means no exception will escape, but regardless you still need to play by the rules (i.e. satisfy the preconditions).

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Sep 6, 2021

https://timsong-cpp.github.io/cppwp/n3337/unique.ptr.single.ctor
Looks like std::unique_ptr<T, D>{} has had UB since C++11 for the right D. Even though it could just have been a call to std::terminate.

@JohelEGP JohelEGP closed this as completed Sep 6, 2021
@jwakely
Copy link
Member

jwakely commented Sep 7, 2021

079f7d3 added noexcept to certain constructors that had the precondition that some initialization didn't throw.

Is that the right commit ID? It didn't add any noexcept.

I agree that making it UB is bad. We should just rely on the compiler to terminate. We should try to reduce the places where programs have unnecessary undefined behaviour.

But I think this needs an lwg issue.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Sep 7, 2021

079f7d3 added noexcept to certain constructors that had the precondition that some initialization didn't throw.

Is that the right commit ID? It didn't add any noexcept.

No.

I agree that making it UB is bad. We should just rely on the compiler to terminate. We should try to reduce the places where programs have unnecessary undefined behaviour.

But I think this needs an lwg issue.

Yes. This seems like a low-hanging fruit compared to other sources of library UB. I'll take a look again and submit a LWG issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants