The rule is that if a declaration has the syntax of a function declaration then it is one; otherwise it is a variable declaration. Surprising instances of this are sometimes called most-vexing-parse.
Bar bar0(Foo0(x), Foo1(x), Foo2(x)); // Does not work: conflicting declaration ‘Foo1 x’ previous declaration as ‘Foo0 x’; conflicting declaration ‘Foo2 x’ previous declaration as ‘Foo0 x’
This is a function declaration: bar0
is the name, Bar
is the return type, and the parameter types are Foo0
, Foo1
and Foo2
. The parameter names are all x
which is illegal - the names of function parameters must be different. If you change x
x
x
to x
y
z
the error goes away).
Bar bar1(Foo0{x}, Foo1(x), Foo2(x)); // Works WTF
Bar bar2(Foo0(x), Foo1{x}, Foo2(x)); // Works WTF
Bar bar4(Foo0{x}, Foo1{x}, Foo2{x}); // Works totally makes sens to me
These lines and create objects bar1
, bar2
, and bar4
of type Bar
. They cannot be parsed as function declarations because the { }
notation is not valid syntax in a function declaration.
Therefore, Foo0{x}
etc. are expressions which provide the arguments to Bar
's constructor. Foo0{x}
and Foo0(x)
are equivalent ways of declaring a temporary of type Foo0
with initializer x
.
Bar bar3(Foo0(x), Foo1(x), Foo2{x}); // Does not work: conflicting declaration ‘Foo1 x’ previous declaration as ‘Foo0 x’
I think this is a compiler bug; the part Foo2{x}
means that this line cannot be a function declaration; and it looks like a valid declaration of a variable bar3
.
x.doStuff(); //Dose not work. This makes sens to me. But in the context its curious
x
is an int
; it does not have any methods.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…