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

[dcl.type.cv] P4 doesn't sufficiently define "modify" #1988

Closed
RedBeard0531 opened this issue Mar 26, 2018 · 3 comments
Closed

[dcl.type.cv] P4 doesn't sufficiently define "modify" #1988

RedBeard0531 opened this issue Mar 26, 2018 · 3 comments
Assignees

Comments

@RedBeard0531
Copy link
Contributor

RedBeard0531 commented Mar 26, 2018

http://eel.is/c++draft/dcl.type.cv#4:

Except that any class member declared mutable can be modified, any attempt to modify a const object during its lifetime results in undefined behavior.

We are debating whether lines 1 and 2 are valid:

struct Empty {
  void doesThisModify() /* not const */ {}
  virtual void whatIfVirtual() /* not const */{}
}

constexpr Empty empty;

const_cast<Empty&>(empty).doesThisModify(); // #1
const_cast<Empty&>(empty).whatIfVirtual(); // #2

The real world case involves the trivial implementation of an interface with non-const virtual methods. I'd like to declare an instance of that constexpr to ensure it happens before dynamic init, but I couldn't prove if it was valid to cast-away constness on the variable to return a non-const pointer to the interface.

@RedBeard0531
Copy link
Contributor Author

I realized I forgot to clearly state the questions: Is calling a non-const method considered "modifying a const object" for the purposes of that clause, or does it actually have to modify some object of fundamental type? Does the virtualness of the method matter?

@jensmaurer
Copy link
Member

"modifying an object" means applying one of [expr.ass], [expr.post.incr], [expr.pre.incr] to the object or to one of its subobjects. See also [expr.const]p2.17. I'm not seeing an urgent defect, but we could make [expr.ass] p2 clearer by saying "modifies the object referred to by the left operand by replacing the object's value with the value of the right operand". Also, we might consider adding the above list of cross-references when we normatively talk about "modify a const object".

@languagelawyer
Copy link
Contributor

languagelawyer commented Apr 23, 2018

"modifying an object" means applying one of [expr.ass], [expr.post.incr], [expr.pre.incr] to the object or to one of its subobjects.

This definition is obvious, but is it explicitly stated in the standard?

@jensmaurer jensmaurer removed the decision-required A decision of the editorial group (or the Project Editor) is required. label Sep 10, 2019
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

3 participants