[Bug c++/19503] Parsing problem in the constructor call of temporary object

gdr at integrable-solutions dot net gcc-bugzilla@gcc.gnu.org
Tue Jan 18 13:42:00 GMT 2005


------- Additional Comments From gdr at integrable-solutions dot net  2005-01-18 13:40 -------
Subject: Re:  New: Parsing problem in the constructor call of temporary object

"timo dot erkkila at tut dot fi" <gcc-bugzilla@gcc.gnu.org> writes:

| Hello,
| 
| the following code
| 
|  struct A { };
|  struct B { B( A& ) { } };
| 
|  void foo( A& a ) { B( a ); }
| 
| results in this output:
|  g++ t.cc -c
| 
|  t.cc: In function `void foo(A&)':
|  t.cc:4: error: declaration of 'B a' shadows a parameter
|  t.cc:4: error: no matching function for call to `B::B()'
|  t.cc:2: note: candidates are: B::B(const B&)
|  t.cc:2: note:                 B::B(A&)
| 
| With a workaround (extra cast) in foo, it however compiles ok:
| 
|  void foo( A& a ) { B( (A&)a ); }

That is no error in the compiler.  
There is an ambiguity in the C++ grammar between declarations and
expression-statements.  The C++ standard rules that the ambiguity be
resolved in favor of declaration.  Therefore

    B(a);

is to be parsed as the declaration of a variable named "a", that requires
default-initialization (hence the second set of diagnostics).  Notice
that the redundant parenthesis around "a" is inherited from C.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19503



More information about the Gcc-bugs mailing list