Skip to content

CWG2661 [class.mem] missing disambiguation rule for pure-specifier vs brace-or-equal-initializer #146

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

Closed
zygoloid opened this issue Oct 7, 2022 · 1 comment · Fixed by cplusplus/draft#6906

Comments

@zygoloid
Copy link
Member

zygoloid commented Oct 7, 2022

Issue description

We have an ambiguous grammar for member-declarator:

member-declarator:

  • declarator virt-specifier-seq[opt] pure-specifier[opt]
  • declarator brace-or-equal-initializer[opt]

pure-specifier:

  • = 0

The primary issue here is that foo = 0 matches both member-declarator productions. Secondarily, a declarator by itself is also ambiguous, but that's easily fixed by removing the [opt] from the second production and doesn't lead to two different program interpretations.

We then have:

  1. A brace-or-equal-initializer shall appear only in the declaration of a data member.
  2. A pure-specifier shall be used only in the declaration of a virtual function that is not a friend declaration.

... which make the two mutually exclusive but isn't itself a disambiguation rule.

Utterances like virtual FunctionType f = 0; are valid, so we can't disambiguate based on the syntactic form of the declarator. In practice, implementations disambiguate based on whether decl-specifier-seq declarator declares a name with function type, and I believe that is the intent based on the rule in [temp.spec] that acquiring a function type through a dependent type is ill-formed.

Suggested resolution:

Option 1

Add a disambiguation rule, such as:

The token sequence = 0 is treated as a pure-specifier if the type of the declarator-id [dcl.meaning.general] is a function type, and is otherwise treated as a brace-or-equal-initializer. [Note: If the member declaration acquires a function type through template instantiation, the program is ill-formed; see [temp.spec.general].]

... and remove the other ambiguity with a grammar change:

member-declarator:

  • declarator virt-specifier-seq[opt] pure-specifier[opt]
  • declarator brace-or-equal-initializer[opt]

Option 2

Change the grammar to remove the ambiguity entirely:

member-declarator:

  • declarator virt-specifier-seq[opt] pure-specifier[opt] brace-or-equal-initializer[opt]
  • declarator brace-or-equal-initializer[opt]

pure-specifier:

  • = 0
  1. A brace-or-equal-initializer shall appear only in the declaration of a data member or a function. [...]
  2. [...] . A brace-or-equal-initializer appearing in the declaration of a member function shall be of the form = 0, and is called a pure-specifier. A pure-specifier shall be used only in the declaration of a virtual function that is not a friend declaration.

Either way, this seems borderline editorial to me.

@jensmaurer jensmaurer changed the title [class.mem] missing disambiguation rule for pure-specifier vs brace-or-equal-initializer CWG2661 [class.mem] missing disambiguation rule for pure-specifier vs brace-or-equal-initializer Dec 4, 2022
@jensmaurer
Copy link
Member

CWG2661

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.

2 participants