Files
2025-10-25 03:02:53 +03:00

94 lines
5.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[stmt.ambig]
# 8 Statements [[stmt]](./#stmt)
## 8.11 Ambiguity resolution [stmt.ambig]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L1367)
There is an ambiguity in the grammar involving[*expression-statement*](stmt.expr#nt:expression-statement "8.3Expression statement[stmt.expr]")*s* and [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]")*s*: An[*expression-statement*](stmt.expr#nt:expression-statement "8.3Expression statement[stmt.expr]") with a [function-style explicit type
conversion](expr.type.conv "7.6.1.4Explicit type conversion (functional notation)[expr.type.conv]") as its leftmost subexpression can be
indistinguishable from a [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]") where the first[*declarator*](dcl.decl.general#nt:declarator "9.3.1General[dcl.decl.general]") starts with a ([.](#1.sentence-1)
In those cases the[*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]") is considered a [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]"),
except as specified below[.](#1.sentence-2)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L1377)
[*Note [1](#note-1)*:
If the [*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]") cannot syntactically be a[*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]"), there is no ambiguity,
so this rule does not apply[.](#2.sentence-1)
In some cases, the whole [*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]") needs to be examined
to determine whether this is the case[.](#2.sentence-2)
This resolves the meaning
of many examples[.](#2.sentence-3)
[*Example [1](#example-1)*:
Assuming T is a[*simple-type-specifier*](dcl.type.simple#nt:simple-type-specifier "9.2.9.3Simple type specifiers[dcl.type.simple]") ([[dcl.type.simple]](dcl.type.simple "9.2.9.3Simple type specifiers")),
T(a)->m = 7; // expression-statement T(a)++; // expression-statement T(a,5)<<c; // expression-statement T(*d)(int); // declaration T(e)[5]; // declaration T(f) = { 1, 2 }; // declaration T(*g)(double(3)); // declaration
In the last example above, g, which is a pointer to T,
is initialized to double(3)[.](#2.sentence-5)
This is of course ill-formed for
semantic reasons, but that does not affect the syntactic analysis[.](#2.sentence-6)
— *end example*]
The remaining cases are [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]")*s*[.](#2.sentence-7)
[*Example [2](#example-2)*: class T {// ...public: T();
T(int);
T(int, int);};
T(a); // declaration T(*b)(); // declaration T(c)=7; // declaration T(d),e,f=3; // declarationextern int h;
T(g)(h,2); // declaration — *end example*]
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L1425)
The disambiguation is purely syntactic; that is, the meaning of the
names occurring in such a statement, beyond whether they are[*type-name*](dcl.type.simple#nt:type-name "9.2.9.3Simple type specifiers[dcl.type.simple]")*s* or not, is not generally used in or changed by the
disambiguation[.](#3.sentence-1)
Class templates are instantiated as necessary to
determine if a qualified name is a [*type-name*](dcl.type.simple#nt:type-name "9.2.9.3Simple type specifiers[dcl.type.simple]")[.](#3.sentence-2)
Disambiguation
precedes parsing, and a statement disambiguated as a declaration may be
an ill-formed declaration[.](#3.sentence-3)
If, during parsing, lookup finds
that a name in a template argument is bound to
(part of) the declaration being parsed,
the program is ill-formed[.](#3.sentence-4)
No diagnostic is required[.](#3.sentence-5)
[*Example [3](#example-3)*: struct T1 { T1 operator()(int x) { return T1(x); }int operator=(int x) { return x; } T1(int) { }};struct T2 { T2(int) { } };int a, (*(*b)(T2))(int), c, d;
void f() {// disambiguation requires this to be parsed as a declaration: T1(a) = 3,
T2(4), // T2 will be declared as a variable of type T1, but this will not(*(*b)(T2(c)))(int(d)); // allow the last part of the declaration to parse properly,// since it depends on T2 being a type-name} — *end example*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L1458)
A syntactically ambiguous statement that can syntactically be
a [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]") with an outermost [*declarator*](dcl.decl.general#nt:declarator "9.3.1General[dcl.decl.general]") with a [*trailing-return-type*](dcl.decl.general#nt:trailing-return-type "9.3.1General[dcl.decl.general]") is considered a [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]") only if it starts with auto[.](#4.sentence-1)
[*Example [4](#example-4)*: struct M;struct S { S* operator()(); int N; int M; void mem(S s) {auto(s)()->M; // expression, S::M hides ::M}};
void f(S s) {{auto(s)()->N; // expressionauto(s)()->M; // function declaration}{ S(s)()->N; // expression S(s)()->M; // expression}} — *end example*]