Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #4625 > unrolled thread
| Started by | J Cook <joecook@gmail.com> |
|---|---|
| First post | 2011-05-03 09:51 -0700 |
| Last post | 2011-05-03 19:31 +0200 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.c++
Strange gcc compiler error? J Cook <joecook@gmail.com> - 2011-05-03 09:51 -0700
Re: Strange gcc compiler error? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-03 13:01 -0400
Re: Strange gcc compiler error? J Cook <joecook@gmail.com> - 2011-05-03 10:06 -0700
Re: Strange gcc compiler error? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-03 13:09 -0400
Re: Strange gcc compiler error? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-03 13:10 -0400
Re: Strange gcc compiler error? Öö Tiib <ootiib@hot.ee> - 2011-05-03 10:36 -0700
Re: Strange gcc compiler error? James Kanze <james.kanze@gmail.com> - 2011-05-03 15:02 -0700
Re: Strange gcc compiler error? Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-05-03 19:31 +0200
| From | J Cook <joecook@gmail.com> |
|---|---|
| Date | 2011-05-03 09:51 -0700 |
| Subject | Strange gcc compiler error? |
| Message-ID | <47dc9f45-a4a5-4b61-aaf2-92c1602cc1cf@i39g2000prd.googlegroups.com> |
Can anyone explain this error? It seems as if the compiler is using
the name of the first parameter as the temporary object name?
class Foo
{
Foo(int s) {}
};
void foo()
{
int test = 4;
Foo(test); // create a temporary. Thinks it is named "test" ??
}
int main()
{
foo();
}
# In function 'void foo()'
# error: conflicting declaration 'Foo test'
# 'test' has a previous declaration as 'int test'
gcc 4.1.2
Thanks in advance,
JC
[toc] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-05-03 13:01 -0400 |
| Message-ID | <ippcd5$5a7$1@dont-email.me> |
| In reply to | #4625 |
On 5/3/2011 12:51 PM, J Cook wrote:
> Can anyone explain this error? It seems as if the compiler is using
> the name of the first parameter as the temporary object name?
>
> class Foo
> {
> Foo(int s) {}
>
> };
>
> void foo()
> {
> int test = 4;
> Foo(test); // create a temporary. Thinks it is named "test" ??
> }
>
> int main()
> {
> foo();
> }
>
> # In function 'void foo()'
> # error: conflicting declaration 'Foo test'
> # 'test' has a previous declaration as 'int test'
>
> gcc 4.1.2
In order to verify you could use www.comeaucomputing.com/tryitout (they
compile but don't link), their compiler is very Standard-compliant.
As to your "error" since 'Foo' is the name of the type, the compiler can
(and will) interpret
Foo(test);
as a declaration of a variable 'test' of type 'Foo', thus trying to
redeclare 'test' with another type. To avoid that add parentheses
Foo((test));
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | J Cook <joecook@gmail.com> |
|---|---|
| Date | 2011-05-03 10:06 -0700 |
| Message-ID | <7e9fe42d-aff5-498f-947e-adef658217bd@r27g2000prr.googlegroups.com> |
| In reply to | #4627 |
On May 3, 1:01 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote: > On 5/3/2011 12:51 PM, J Cook wrote: \> In order to verify you could usewww.comeaucomputing.com/ tryitout(they > compile but don't link), their compiler is very Standard-compliant. > > As to your "error" since 'Foo' is the name of the type, the compiler can > (and will) interpret > > Foo(test); > > as a declaration of a variable 'test' of type 'Foo', thus trying to > redeclare 'test' with another type. To avoid that add parentheses > > Foo((test)); You are right about the semantic confusion. I had thought of the double parenthesis would fix it also, but it does not seem to make any difference. I really want to create an unnamed temporary (a common task), and can't seem to find a way to hint the parser that this should be a Constructor call with one parameter. Thanks, --JC
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-05-03 13:09 -0400 |
| Message-ID | <ippcrt$9bl$1@dont-email.me> |
| In reply to | #4628 |
On 5/3/2011 1:06 PM, J Cook wrote:
> On May 3, 1:01 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>> On 5/3/2011 12:51 PM, J Cook wrote:
> \> In order to verify you could usewww.comeaucomputing.com/
> tryitout(they
>> compile but don't link), their compiler is very Standard-compliant.
>>
>> As to your "error" since 'Foo' is the name of the type, the compiler can
>> (and will) interpret
>>
>> Foo(test);
>>
>> as a declaration of a variable 'test' of type 'Foo', thus trying to
>> redeclare 'test' with another type. To avoid that add parentheses
>>
>> Foo((test));
>
> You are right about the semantic confusion. I had thought of the
> double parenthesis would fix it also, but it does not seem to make any
> difference. I really want to create an unnamed temporary (a common
> task), and can't seem to find a way to hint the parser that this
> should be a Constructor call with one parameter.
You can do
static_cast<Foo>(test);
that should invoke the constructor.
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-05-03 13:10 -0400 |
| Message-ID | <ippcua$9bl$2@dont-email.me> |
| In reply to | #4629 |
On 5/3/2011 1:09 PM, Victor Bazarov wrote:
> On 5/3/2011 1:06 PM, J Cook wrote:
>> On May 3, 1:01 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>>> On 5/3/2011 12:51 PM, J Cook wrote:
>> \> In order to verify you could usewww.comeaucomputing.com/
>> tryitout(they
>>> compile but don't link), their compiler is very Standard-compliant.
>>>
>>> As to your "error" since 'Foo' is the name of the type, the compiler can
>>> (and will) interpret
>>>
>>> Foo(test);
>>>
>>> as a declaration of a variable 'test' of type 'Foo', thus trying to
>>> redeclare 'test' with another type. To avoid that add parentheses
>>>
>>> Foo((test));
>>
>> You are right about the semantic confusion. I had thought of the
>> double parenthesis would fix it also, but it does not seem to make any
>> difference. I really want to create an unnamed temporary (a common
>> task), and can't seem to find a way to hint the parser that this
>> should be a Constructor call with one parameter.
>
> You can do
>
> static_cast<Foo>(test);
>
> that should invoke the constructor.
Or, another way is to cast:
(Foo)test;
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2011-05-03 10:36 -0700 |
| Message-ID | <a8fdb6a8-eef7-4a06-8fff-fea7519828e2@p18g2000yqj.googlegroups.com> |
| In reply to | #4630 |
On May 3, 8:10 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote: > On 5/3/2011 1:09 PM, Victor Bazarov wrote: > > > > > > > On 5/3/2011 1:06 PM, J Cook wrote: > >> On May 3, 1:01 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote: > >>> On 5/3/2011 12:51 PM, J Cook wrote: > >> \> In order to verify you could usewww.comeaucomputing.com/ > >> tryitout(they > >>> compile but don't link), their compiler is very Standard-compliant. > > >>> As to your "error" since 'Foo' is the name of the type, the compiler can > >>> (and will) interpret > > >>> Foo(test); > > >>> as a declaration of a variable 'test' of type 'Foo', thus trying to > >>> redeclare 'test' with another type. To avoid that add parentheses > > >>> Foo((test)); > > >> You are right about the semantic confusion. I had thought of the > >> double parenthesis would fix it also, but it does not seem to make any > >> difference. I really want to create an unnamed temporary (a common > >> task), and can't seem to find a way to hint the parser that this > >> should be a Constructor call with one parameter. > > > You can do > > > static_cast<Foo>(test); > > > that should invoke the constructor. > > Or, another way is to cast: > > (Foo)test; Still does not work since Foo constructor is private in example code. If it is made public and explicit (that it usually should be with just one parameter) then such monster works: (void)Foo(test);
[toc] | [prev] | [next] | [standalone]
| From | James Kanze <james.kanze@gmail.com> |
|---|---|
| Date | 2011-05-03 15:02 -0700 |
| Message-ID | <d91257b3-524b-45cb-b651-542b660fe4d8@p6g2000vbn.googlegroups.com> |
| In reply to | #4632 |
On May 3, 6:36 pm, Öö Tiib <oot...@hot.ee> wrote:
> On May 3, 8:10 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
>
>
>
> > On 5/3/2011 1:09 PM, Victor Bazarov wrote:
>
> > > On 5/3/2011 1:06 PM, J Cook wrote:
> > >> On May 3, 1:01 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
> > >>> On 5/3/2011 12:51 PM, J Cook wrote:
> > >> \> In order to verify you could usewww.comeaucomputing.com/
> > >> tryitout(they
> > >>> compile but don't link), their compiler is very Standard-compliant.
>
> > >>> As to your "error" since 'Foo' is the name of the type, the compiler can
> > >>> (and will) interpret
>
> > >>> Foo(test);
>
> > >>> as a declaration of a variable 'test' of type 'Foo', thus trying to
> > >>> redeclare 'test' with another type. To avoid that add parentheses
>
> > >>> Foo((test));
>
> > >> You are right about the semantic confusion. I had thought of the
> > >> double parenthesis would fix it also, but it does not seem to make any
> > >> difference. I really want to create an unnamed temporary (a common
> > >> task), and can't seem to find a way to hint the parser that this
> > >> should be a Constructor call with one parameter.
>
> > > You can do
>
> > > static_cast<Foo>(test);
>
> > > that should invoke the constructor.
>
> > Or, another way is to cast:
>
> > (Foo)test;
>
> Still does not work since Foo constructor is private in example code.
> If it is made public and explicit (that it usually should be with just
> one parameter) then such monster works:
>
> (void)Foo(test);
Or simply putting the entire expression in parentheses:
(Foo( test ));
(But it sure looks funny:-).)
--
James Kanze
[toc] | [prev] | [next] | [standalone]
| From | Kai-Uwe Bux <jkherciueh@gmx.net> |
|---|---|
| Date | 2011-05-03 19:31 +0200 |
| Message-ID | <ippe4t$678$1@hoshi.visyn.net> |
| In reply to | #4625 |
J Cook wrote:
> Can anyone explain this error? It seems as if the compiler is using
> the name of the first parameter as the temporary object name?
>
> class Foo
> {
> Foo(int s) {}
>
> };
>
> void foo()
> {
> int test = 4;
> Foo(test); // create a temporary. Thinks it is named "test" ??
> }
>
> int main()
> {
> foo();
> }
>
> # In function 'void foo()'
> # error: conflicting declaration 'Foo test'
> # 'test' has a previous declaration as 'int test'
[...]
The compiler treats the marked line not as creating a temporary but as a
variable declaration of the variable test. At least, that is what the error
message says.
What one would expect, is a message: "constructor Foo(int) is private and
cannot be used in this context.". However, after discarding the private
constructor, the compiler goes on and tries the default constructor. That's
why you get a redefinition error message. The compiler does this, because
clause [8.3/6] says that one way of reading
Foo(test);
is:
Foo test;
Best,
Kai-Uwe Bux
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web