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

Document if mut is UB #422

Closed
libbooze opened this issue Feb 22, 2021 · 3 comments
Closed

Document if mut is UB #422

libbooze opened this issue Feb 22, 2021 · 3 comments

Comments

@libbooze
Copy link

libbooze commented Feb 22, 2021

I am not a language lawyer, but I think it would be nice if mut was documented in a sense that if it is technically UB(as used in examples in tutorial), but it works in practice then that is is mentioned (in docs or code comments).

cppref says this:

const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

Apologies in advance if I misunderstood the cppref quote.

@JohelEGP
Copy link
Contributor

JohelEGP commented Jul 3, 2021

It isn't UB as used in the examples. That said, mut is much too powerful. I would stick to specifying the lambda as mutable if all you need is to modify a lambda's capture for a test (as opposed to non-testing uses, as mutable affects all captures, which might have unintended consequences. See https://wg21.link/P2034).

@libbooze
Copy link
Author

libbooze commented Jul 9, 2021

Apologies, this is all very weird to me, but you are right, I always assumed that writing to any const object is UB, unless it was a const ref to something that was originally mutable.

Feel free to close this issue.

@libbooze
Copy link
Author

libbooze commented Jul 9, 2021

In fact I will close it, since I presume this is resolve. :)

@libbooze libbooze closed this as completed Jul 9, 2021
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

No branches or pull requests

2 participants