Type conversion of reference to a temporary object

Discussion in 'C++' started by Stefan Strasser, Mar 28, 2005.

  1. JH Trauntvein schrieb:

    > void foo(std::istream &in)
    > {
    > int i, j;
    > in >> i >> j;
    > } // foo
    >
    >
    > int main()
    > {
    > foo(std::istringstream("1 1"));
    > return 0;
    > } // main
    >
    >
    > g++ gives the following complaint when I attempt to compile this code:
    >
    > error: invalid initialization of non-const reference of type
    > 'std::istream &' from a temporary of type 'std::istringstream'
    >
    > I was under the impression that my code above was standard-conforming.
    > Am I wrong on this point?


    calling a constructor creates a temporary. temporaries are const. your
    parameter of "foo" is not. you could pass it by value to make it work e.g.
    since vs .net seems to be very standard conforming I guess this is some
    sort of extension which can be turned off.
     
    Stefan Strasser, Mar 28, 2005
    #1
    1. Advertising

  2. I have something like the following code that I am trying to convert
    from vs studio .net to g++ under linux:

    #include <sstream>
    #include <iostream>


    void foo(std::istream &in)
    {
    int i, j;
    in >> i >> j;
    } // foo


    int main()
    {
    foo(std::istringstream("1 1"));
    return 0;
    } // main


    g++ gives the following complaint when I attempt to compile this code:

    error: invalid initialization of non-const reference of type
    'std::istream &' from a temporary of type 'std::istringstream'

    I was under the impression that my code above was standard-conforming.
    Am I wrong on this point?

    Regards,

    Jon Trauntvein
     
    JH Trauntvein, Mar 28, 2005
    #2
    1. Advertising

  3. JH Trauntvein wrote:
    > I have something like the following code that I am trying to convert
    > from vs studio .net to g++ under linux:
    >
    > #include <sstream>
    > #include <iostream>
    >
    >
    > void foo(std::istream &in)
    > {
    > int i, j;
    > in >> i >> j;
    > } // foo
    >
    >
    > int main()
    > {
    > foo(std::istringstream("1 1"));
    > return 0;
    > } // main
    >
    >
    > g++ gives the following complaint when I attempt to compile this code:
    >
    > error: invalid initialization of non-const reference of type
    > 'std::istream &' from a temporary of type 'std::istringstream'
    >
    > I was under the impression that my code above was standard-conforming.
    > Am I wrong on this point?


    Yes. You create a temporary of type 'std::istringstream' in the main
    function, and references to non-const cannot be bound to a temporary.
    VC++ allows that as an extension. You will have to do

    std::istringstream is("1 1");
    foo(is);

    V
     
    Victor Bazarov, Mar 28, 2005
    #3
    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. John Ky
    Replies:
    9
    Views:
    445
    John Ky
    Feb 23, 2004
  2. Dave Rahardja
    Replies:
    15
    Views:
    1,180
    Alf P. Steinbach
    Nov 3, 2005
  3. Raider
    Replies:
    3
    Views:
    357
    Frederick Gotham
    Jun 22, 2006
  4. Martin Rennix
    Replies:
    3
    Views:
    649
    Martin Rennix
    May 7, 2007
  5. Replies:
    7
    Views:
    3,264
    James Kanze
    Feb 12, 2008
Loading...

Share This Page