The following codes are quoted from C++11 standard N3485 Section 6.8.3 about ambiguity resolution:
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
(*(*b)(T2(c)))(int(d)); // a variable of type T1
// but this will not allow
// the last part of the
// declaration to parse
// properly since it depends
// on T2 being a type-name
}
I have no idea about how to parse this code:
T1(a) = 3,
T2(4),
(*(*b)(T2(c)))(int(d));
What does it mean in this case? Could you explain it to me? This example code seems rather obscure to me.
Thank you very much.
It means that when something can be parsed as a declaration, it should be parsed as a declaration. In this case,
T1(a) = 3
Is a valid declaration, and it declares an object a of type T1, initialized from the value 3 (rather than constructing a temporary of type T1 initialized from a, then assigning 3 to that temporary). This is, in fact, equivalent to the following:
T1 a = 3
Now the remaining declarators:
T2(4)
Here T2 is the name of an object of type T1 initialized from the value 4, as in:
T1 a = 3, T2(4)
So T2 is the name of the object, not of a type, similarly to the case below:
int x = 42, y(1729)
Where x is an object of type int initialized from 42, and y is another object of type int initialized from the value 1729.
Then the last declarator cannot be parsed correctly, because T2 is not the name of a type, but the name of an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With