This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/19503] Parsing problem in the constructor call of temporary object
- From: "gdr at integrable-solutions dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 Jan 2005 13:40:49 -0000
- Subject: [Bug c++/19503] Parsing problem in the constructor call of temporary object
- References: <20050118122601.19503.timo.erkkila@tut.fi>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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