Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Temporary creation vs. variable declaration

Reply
Thread Tools

Re: Temporary creation vs. variable declaration

 
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"Fabian Schmied" <(E-Mail Removed)> wrote in message
news:bfdo6o$dqq71$(E-Mail Removed)-berlin.de...
> I guess this has been discussed before, maybe one of you could point me in
> the right direction. Please consider the following code:
>
> #include <iostream>
> using namespace std;
>
> class C {
> public:
> C() {
> }
>
> C(int i) {
> }
> };
>
> int main() {
> int i = 10;
> C(5); // 1
> C((int)i); // 2
> C(i); // 3
> return 0;
> }
>
> Why does 3 denote a local variable declaration, whereas 1 and 2 cause the
> creation of temporaries? (At least it is that way in both Microsoft Visual
> C++ and GCC.)
>
> Fabian Schmied
>


There a rule in C++, something to the effect that if a statement can be
interpreted as a declaration or an expression then a declaration is
preferred. 1 and 2 clearly must be expressions but 3 could be an expression
or a declaration, a declaration is preferred.

john


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-20-2003
Rolf Magnus wrote:

> John Harrison wrote:
>
>>
>> "Fabian Schmied" <(E-Mail Removed)> wrote in message
>> news:bfdo6o$dqq71$(E-Mail Removed)-berlin.de...
>>> I guess this has been discussed before, maybe one of you could point
>>> me in the right direction. Please consider the following code:
>>>
>>> #include <iostream>
>>> using namespace std;
>>>
>>> class C {
>>> public:
>>> C() {
>>> }
>>>
>>> C(int i) {
>>> }
>>> };
>>>
>>> int main() {
>>> int i = 10;
>>> C(5); // 1
>>> C((int)i); // 2
>>> C(i); // 3
>>> return 0;
>>> }
>>>
>>> Why does 3 denote a local variable declaration, whereas 1 and 2
>>> cause the creation of temporaries? (At least it is that way in both
>>> Microsoft Visual C++ and GCC.)
>>>
>>> Fabian Schmied
>>>

>>
>> There a rule in C++, something to the effect that if a statement can
>> be interpreted as a declaration or an expression then a declaration
>> is preferred. 1 and 2 clearly must be expressions but 3 could be an
>> expression or a declaration, a declaration is preferred.

>
> How would 3 be a declaration? i isn't a type, so it can't be a
> parameter.


Oh, and also, there is no return type.

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bfe09o$9f9$03$(E-Mail Removed)-online.com...
> Rolf Magnus wrote:
>
> > John Harrison wrote:
> >
> >>
> >> "Fabian Schmied" <(E-Mail Removed)> wrote in message
> >> news:bfdo6o$dqq71$(E-Mail Removed)-berlin.de...
> >>> I guess this has been discussed before, maybe one of you could point
> >>> me in the right direction. Please consider the following code:
> >>>
> >>> #include <iostream>
> >>> using namespace std;
> >>>
> >>> class C {
> >>> public:
> >>> C() {
> >>> }
> >>>
> >>> C(int i) {
> >>> }
> >>> };
> >>>
> >>> int main() {
> >>> int i = 10;
> >>> C(5); // 1
> >>> C((int)i); // 2
> >>> C(i); // 3
> >>> return 0;
> >>> }
> >>>
> >>> Why does 3 denote a local variable declaration, whereas 1 and 2
> >>> cause the creation of temporaries? (At least it is that way in both
> >>> Microsoft Visual C++ and GCC.)
> >>>
> >>> Fabian Schmied
> >>>
> >>
> >> There a rule in C++, something to the effect that if a statement can
> >> be interpreted as a declaration or an expression then a declaration
> >> is preferred. 1 and 2 clearly must be expressions but 3 could be an
> >> expression or a declaration, a declaration is preferred.

> >
> > How would 3 be a declaration? i isn't a type, so it can't be a
> > parameter.

>
> Oh, and also, there is no return type.
>


i isn't a type, is the name of the variable being declared. The above code
should produce a 'variable i declared twice' error message

john


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-20-2003
John Harrison wrote:

>
> "Rolf Magnus" <(E-Mail Removed)> wrote in message
> news:bfe09o$9f9$03$(E-Mail Removed)-online.com...
>> Rolf Magnus wrote:
>>
>> > John Harrison wrote:
>> >
>> >>
>> >> "Fabian Schmied" <(E-Mail Removed)> wrote in
>> >> message news:bfdo6o$dqq71$(E-Mail Removed)-berlin.de...
>> >>> I guess this has been discussed before, maybe one of you could
>> >>> point me in the right direction. Please consider the following
>> >>> code:
>> >>>
>> >>> #include <iostream>
>> >>> using namespace std;
>> >>>
>> >>> class C {
>> >>> public:
>> >>> C() {
>> >>> }
>> >>>
>> >>> C(int i) {
>> >>> }
>> >>> };
>> >>>
>> >>> int main() {
>> >>> int i = 10;
>> >>> C(5); // 1
>> >>> C((int)i); // 2
>> >>> C(i); // 3
>> >>> return 0;
>> >>> }
>> >>>
>> >>> Why does 3 denote a local variable declaration, whereas 1 and 2
>> >>> cause the creation of temporaries? (At least it is that way in
>> >>> both Microsoft Visual C++ and GCC.)
>> >>>
>> >>> Fabian Schmied
>> >>>
>> >>
>> >> There a rule in C++, something to the effect that if a statement
>> >> can be interpreted as a declaration or an expression then a
>> >> declaration is preferred. 1 and 2 clearly must be expressions but
>> >> 3 could be an expression or a declaration, a declaration is
>> >> preferred.
>> >
>> > How would 3 be a declaration? i isn't a type, so it can't be a
>> > parameter.

>>
>> Oh, and also, there is no return type.
>>

>
> i isn't a type, is the name of the variable being declared.


Why?

> The above code should produce a 'variable i declared twice' error
> message


How does "C(i);" declare i as a new variable? Or is it the same
as "C i;"

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bfe7cc$1ld$06$(E-Mail Removed)-online.com...
> John Harrison wrote:
>
> >
> > "Rolf Magnus" <(E-Mail Removed)> wrote in message
> > news:bfe09o$9f9$03$(E-Mail Removed)-online.com...
> >> Rolf Magnus wrote:
> >>
> >> > John Harrison wrote:
> >> >
> >> >>
> >> >> "Fabian Schmied" <(E-Mail Removed)> wrote in
> >> >> message news:bfdo6o$dqq71$(E-Mail Removed)-berlin.de...
> >> >>> I guess this has been discussed before, maybe one of you could
> >> >>> point me in the right direction. Please consider the following
> >> >>> code:
> >> >>>
> >> >>> #include <iostream>
> >> >>> using namespace std;
> >> >>>
> >> >>> class C {
> >> >>> public:
> >> >>> C() {
> >> >>> }
> >> >>>
> >> >>> C(int i) {
> >> >>> }
> >> >>> };
> >> >>>
> >> >>> int main() {
> >> >>> int i = 10;
> >> >>> C(5); // 1
> >> >>> C((int)i); // 2
> >> >>> C(i); // 3
> >> >>> return 0;
> >> >>> }
> >> >>>
> >> >>> Why does 3 denote a local variable declaration, whereas 1 and 2
> >> >>> cause the creation of temporaries? (At least it is that way in
> >> >>> both Microsoft Visual C++ and GCC.)
> >> >>>
> >> >>> Fabian Schmied
> >> >>>
> >> >>
> >> >> There a rule in C++, something to the effect that if a statement
> >> >> can be interpreted as a declaration or an expression then a
> >> >> declaration is preferred. 1 and 2 clearly must be expressions but
> >> >> 3 could be an expression or a declaration, a declaration is
> >> >> preferred.
> >> >
> >> > How would 3 be a declaration? i isn't a type, so it can't be a
> >> > parameter.
> >>
> >> Oh, and also, there is no return type.
> >>

> >
> > i isn't a type, is the name of the variable being declared.

>
> Why?
>
> > The above code should produce a 'variable i declared twice' error
> > message

>
> How does "C(i);" declare i as a new variable? Or is it the same
> as "C i;"
>


Exactly, its the same as C i;

Sometimes you have to put parens in a variable declaration, e.g.

int (*a)[10];

but you are also allowed to put unnecessary parens.

int (a);

john


 
Reply With Quote
 
Michael Kochetkov
Guest
Posts: n/a
 
      07-20-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bfe7cc$1ld$06$(E-Mail Removed)-online.com...
> > i isn't a type, is the name of the variable being declared.

>
> Why?
>
> > The above code should produce a 'variable i declared twice' error
> > message

>
> How does "C(i);" declare i as a new variable? Or is it the same
> as "C i;"

John is absolutely right. Ambiguities in C++ are resolved in favour of
declarations and a declarator may be enclosed in parantheses indeed.

--
With regards,
Michael Kochetkov.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Preventing creation of temporary RAII objects tomthemighty@googlemail.com C++ 6 10-10-2006 09:01 PM
temporary object creation question xllx.relient.xllx@gmail.com C++ 2 07-08-2006 12:29 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM



Advertisments