Passing a temporary object to a reference parameter

Discussion in 'C++' started by Martin Rennix, May 6, 2007.

  1. I have a function declared as:

    void foo(T&);

    And I'm trying to pass a Bar object by doing this:

    foo(T());

    My compiler (VC++ 2005) gives me a warning "nonstandard extension
    used: conversion from T to T&.
    It compiles successfully anayway, but I assume will not compile on
    other compilers?

    If I do:

    T bar;
    foo(bar);

    it compiles without the warning.

    So...my question is do I need to define named variables to pass to
    reference parameters in order to remain within the standard?

    Martin
     
    Martin Rennix, May 6, 2007
    #1
    1. Advertising

  2. Martin Rennix

    Rolf Magnus Guest

    Martin Rennix wrote:

    > I have a function declared as:
    >
    > void foo(T&);
    >
    > And I'm trying to pass a Bar object by doing this:
    >
    > foo(T());
    >
    > My compiler (VC++ 2005) gives me a warning "nonstandard extension
    > used: conversion from T to T&.
    > It compiles successfully anayway, but I assume will not compile on
    > other compilers?


    That's right.

    > If I do:
    >
    > T bar;
    > foo(bar);
    >
    > it compiles without the warning.
    >
    > So...my question is do I need to define named variables to pass to
    > reference parameters in order to remain within the standard?


    For references to non-const, yes.
     
    Rolf Magnus, May 6, 2007
    #2
    1. Advertising

  3. On 2007-05-06 15:33, Rolf Magnus wrote:
    > Martin Rennix wrote:
    >
    >> I have a function declared as:
    >>
    >> void foo(T&);
    >>
    >> And I'm trying to pass a Bar object by doing this:
    >>
    >> foo(T());
    >>
    >> My compiler (VC++ 2005) gives me a warning "nonstandard extension
    >> used: conversion from T to T&.
    >> It compiles successfully anayway, but I assume will not compile on
    >> other compilers?

    >
    > That's right.
    >
    >> If I do:
    >>
    >> T bar;
    >> foo(bar);
    >>
    >> it compiles without the warning.
    >>
    >> So...my question is do I need to define named variables to pass to
    >> reference parameters in order to remain within the standard?

    >
    > For references to non-const, yes.


    Which means that if you change the function to void foo(const T&) it
    will work even when you do foo(T());.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, May 6, 2007
    #3
  4. On May 6, 10:24 pm, Erik Wikström <> wrote:

    > Which means that if you change the function to void foo(const T&) it
    > will work even when you do foo(T());.


    Thanks - that did the trick. I always forget to put in the const
    qualifier. Java background you see ;-)
     
    Martin Rennix, May 7, 2007
    #4
    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:
    468
    John Ky
    Feb 23, 2004
  2. Stefan Strasser
    Replies:
    2
    Views:
    8,212
    Victor Bazarov
    Mar 28, 2005
  3. Dave Rahardja
    Replies:
    15
    Views:
    1,192
    Alf P. Steinbach
    Nov 3, 2005
  4. Replies:
    7
    Views:
    3,329
    James Kanze
    Feb 12, 2008
  5. shaan
    Replies:
    16
    Views:
    428
    Jorgen Grahn
    Dec 25, 2013
Loading...

Share This Page