Construction of temporary in function call is interpreted as declaration

穿精又带淫゛_ 提交于 2019-12-29 08:33:09

问题


Lately I ran into a problem which somehow (but only somehow) makes sense to me. It is based on interpreting the construction of a temporary as declaration of the single (!) constructor argument. Please have a look at the minimal example below.

#include <iostream>

class Foo0{
public:
  Foo0(int a){};
  void doStuff() {std::cout<<"maap"<<std::endl;};
};

class Foo1{
public:
  Foo1(int a){};
  void doStuff() {std::cout<<"maap"<<std::endl;};
};

class Foo2{
public:
  Foo2(int a){};
  void doStuff() {std::cout<<"maap"<<std::endl;};
};

class Bar{
public:
  Bar(Foo0 foo0, Foo1 foo1, Foo2 foo2){};
};

int main () {
  int x = 1;

  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’
  Bar bar1(Foo0{x}, Foo1(x), Foo2(x)); // Works WTF
  Bar bar2(Foo0(x), Foo1{x}, Foo2(x)); // Works WTF
  Bar bar3(Foo0(x), Foo1(x), Foo2{x}); // Does not work: conflicting declaration ‘Foo1 x’ previous declaration as ‘Foo0 x’
  Bar bar4(Foo0{x}, Foo1{x}, Foo2{x}); // Works totally makes sens to me

  x.doStuff(); //Dose not work. This makes sens to me. But in the context its curious though.
}

I already read that expressions like:

Foo(a);

Are interpreted (if there is a standard constructor) as declaration of a. This makes sense and it is totally fine, since you can just use the {}-brackets to make the construction explicit. But what I do not understand is:

  1. Why is there a problem with the construction of bar0? All Foos do not have a standard constructor. So it does not make sense to interpret something like Foo0(x) as declaration of x.

  2. Why does the construction of bar1 and bar2 work? It is obvious to me that the construction of bar4 works, since I use the {}-brackets for all temporary Foos, thus I am explicit about what I want.

  3. If it is only necessary to use the {}-brackets with only one of the Foos to solve the problem... why does the construction of bar3 fail?

  4. Furthermore, x is declared before any Bar is constructed. Why does the compiler not complain about that?

The last question is related to my last line of example code. Long story short: What does the compiler think that I want him to do and where do I miss the appearance of shadowing?

PS: If it is of interest -- I use the gcc-4.9.2.
PPS: I tried the same with bar's constructor taking three Foo0s as arguments. Same story here. But the error says nothing about conflicting declaration but about redefinition of x.


回答1:


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.




回答2:


1) Foo0(x) is treated as a parameter of function bar0 in this instance. Here, it does not matter if it has a standard constructor or not. It's not a local variable declaration and initialization, but just a parameter declaration in a function declaration.

2) My guess is this has something to do with the parsing, but somebody correct me if I'm wrong.. Examples bar1 and bar2 work, because the compiler knows that bar1 and bar2 are local variable declarations (and not function declarations) as soon as it sees the first occurrence of {}. These first occurences of {} appear before x is declared twice as a parameter of the function.

3) The construction of bar3 fails, because the compiler first assumes that bar3 is a function declaration. The function declaration takes three parameters, which are all named x. Obviously, this is not correct.

4) The x in the function declaration is just a name for the parameter. It is in a different scope than the integer x you declared before that.



来源:https://stackoverflow.com/questions/28034742/construction-of-temporary-in-function-call-is-interpreted-as-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!