94 lines
5.1 KiB
Markdown
94 lines
5.1 KiB
Markdown
[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.3 Expression statement [stmt.expr]")*s* and [*declaration*](dcl.pre#nt:declaration "9.1 Preamble [dcl.pre]")*s*: An[*expression-statement*](stmt.expr#nt:expression-statement "8.3 Expression statement [stmt.expr]") with a [function-style explicit type
|
||
conversion](expr.type.conv "7.6.1.4 Explicit type conversion (functional notation) [expr.type.conv]") as its leftmost subexpression can be
|
||
indistinguishable from a [*declaration*](dcl.pre#nt:declaration "9.1 Preamble [dcl.pre]") where the first[*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]") starts with a ([.](#1.sentence-1)
|
||
|
||
In those cases the[*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]") is considered a [*declaration*](dcl.pre#nt:declaration "9.1 Preamble [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.1 Preamble [stmt.pre]") cannot syntactically be a[*declaration*](dcl.pre#nt:declaration "9.1 Preamble [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.1 Preamble [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.3 Simple type specifiers [dcl.type.simple]") ([[dcl.type.simple]](dcl.type.simple "9.2.9.3 Simple 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.1 Preamble [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.3 Simple 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.3 Simple 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.1 Preamble [dcl.pre]") with an outermost [*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]") with a [*trailing-return-type*](dcl.decl.general#nt:trailing-return-type "9.3.1 General [dcl.decl.general]") is considered a [*declaration*](dcl.pre#nt:declaration "9.1 Preamble [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*]
|