Re: Temporary creation vs. variable declaration

Discussion in 'C++' started by John Harrison, Jul 20, 2003.

  1. "Fabian Schmied" <> wrote in message
    news:bfdo6o$dqq71$-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
    John Harrison, Jul 20, 2003
    #1
    1. Advertising

  2. John Harrison

    Rolf Magnus Guest

    Rolf Magnus wrote:

    > John Harrison wrote:
    >
    >>
    >> "Fabian Schmied" <> wrote in message
    >> news:bfdo6o$dqq71$-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.
    Rolf Magnus, Jul 20, 2003
    #2
    1. Advertising

  3. "Rolf Magnus" <> wrote in message
    news:bfe09o$9f9$03$-online.com...
    > Rolf Magnus wrote:
    >
    > > John Harrison wrote:
    > >
    > >>
    > >> "Fabian Schmied" <> wrote in message
    > >> news:bfdo6o$dqq71$-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
    John Harrison, Jul 20, 2003
    #3
  4. John Harrison

    Rolf Magnus Guest

    John Harrison wrote:

    >
    > "Rolf Magnus" <> wrote in message
    > news:bfe09o$9f9$03$-online.com...
    >> Rolf Magnus wrote:
    >>
    >> > John Harrison wrote:
    >> >
    >> >>
    >> >> "Fabian Schmied" <> wrote in
    >> >> message news:bfdo6o$dqq71$-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;"
    Rolf Magnus, Jul 20, 2003
    #4
  5. "Rolf Magnus" <> wrote in message
    news:bfe7cc$1ld$06$-online.com...
    > John Harrison wrote:
    >
    > >
    > > "Rolf Magnus" <> wrote in message
    > > news:bfe09o$9f9$03$-online.com...
    > >> Rolf Magnus wrote:
    > >>
    > >> > John Harrison wrote:
    > >> >
    > >> >>
    > >> >> "Fabian Schmied" <> wrote in
    > >> >> message news:bfdo6o$dqq71$-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
    John Harrison, Jul 20, 2003
    #5
  6. "Rolf Magnus" <> wrote in message
    news:bfe7cc$1ld$06$-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.
    Michael Kochetkov, Jul 20, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. baumann@pan

    variable definiton / variable declaration

    baumann@pan, May 11, 2005, in forum: C Programming
    Replies:
    3
    Views:
    539
    CBFalconer
    May 11, 2005
  2. Bolin
    Replies:
    4
    Views:
    408
  3. Replies:
    2
    Views:
    884
    Frederick Gotham
    Jul 8, 2006
  4. Replies:
    6
    Views:
    338
    Roland Pibinger
    Oct 10, 2006
  5. Replies:
    7
    Views:
    3,211
    James Kanze
    Feb 12, 2008
Loading...

Share This Page