Nonstandard Extension Used?

Discussion in 'C++' started by Nephi Immortal, Feb 1, 2013.

  1. Why do C++ Compiler generates an error to report nonstandard extension? Func() and Func2() do the sequence by putting data into stack and pop them out.

    Sequence:
    Call Func()
    Call Constructor
    Call Copy Constructor
    Call Destructor
    Call Func2() as Data is a reference after copy constructor is created.
    Call Func2() again and uses the same reference.
    Call more deep functions until to the end.
    Return back to Func()
    Call Copy Constructor
    Call Destructor
    Return from Func() to main().
    ‘x’ has data after copy constructor was done.

    The reference is used to avoid to allocate more temporary memory and more numbers of copy constructors are reduced.

    main.cpp(1383): warning C4239: nonstandard extension used : 'argument' : conversion from 'Data' to 'Data &'



    struct Data
    {
    Data() {}
    ~Data() {}
    Data( Data const& r ) {}
    int array_[ 8 ];
    };

    Data Func()
    {
    Data temp;
    return temp;
    }

    Data &Func2( Data &d )
    {
    return d;
    }



    int main()
    {
    Data x = Func2( Func2( Func() ) );

    return 0;
    }
    Nephi Immortal, Feb 1, 2013
    #1
    1. Advertising

  2. On 2/1/2013 2:44 PM, Nephi Immortal wrote:
    >
    > The reference is used to avoid to allocate more temporary memory and more numbers of copy constructors are reduced.
    >
    > main.cpp(1383): warning C4239: nonstandard extension used : 'argument' : conversion from 'Data' to 'Data &'
    >


    C++ prohibits binding non-const references to rvalues (in your case: to
    expressions retuning temporary objects).

    For example, this is invalid

    T &r = T();

    as well as this

    void foo(T& r);

    ...
    foo(T());

    Your code attempts to do the latter.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Feb 1, 2013
    #2
    1. Advertising

  3. Nephi Immortal

    SG Guest

    Am 01.02.2013 23:44, schrieb Nephi Immortal:
    > Why do C++ Compiler generates an error to report nonstandard extension?


    Because you did not write valid ISO C++ Code.

    > The reference is used to avoid to allocate more temporary memory and more numbers of copy constructors are reduced.
    > main.cpp(1383): warning C4239: nonstandard extension used : 'argument' : conversion from 'Data' to 'Data &'


    You tried to initialize a mutable lvalue reference with an rvalue
    expression. This is not allowed according to the ISO C++ standard.

    > Data Func()
    > {
    > Data temp;
    > return temp;
    > }
    >
    > Data &Func2( Data &d )
    > {
    > return d;
    > }
    >
    >
    >
    > int main()
    > {
    > Data x = Func2( Func2( Func() ) );
    >
    > return 0;
    > }
    >


    Func() is an rvalue expression because you return an object by value.
    Func2 takes a mutable lvalue reference.
    This is what's wrong with your program.
    SG, Feb 1, 2013
    #3
  4. On Friday, February 1, 2013 5:28:59 PM UTC-6, SG wrote:
    > Am 01.02.2013 23:44, schrieb Nephi Immortal:
    >
    > > Why do C++ Compiler generates an error to report nonstandard extension?

    >
    >
    >
    > Because you did not write valid ISO C++ Code.
    >
    >
    >
    > > The reference is used to avoid to allocate more temporary memory and more numbers of copy constructors are reduced.

    >
    > > main.cpp(1383): warning C4239: nonstandard extension used : 'argument' : conversion from 'Data' to 'Data &'

    >
    >
    >
    > You tried to initialize a mutable lvalue reference with an rvalue
    >
    > expression. This is not allowed according to the ISO C++ standard.
    >
    >
    >
    > > Data Func()

    >
    > > {

    >
    > > Data temp;

    >
    > > return temp;

    >
    > > }


    Let's revise my code below.

    Data &&Func()
    {
    Data temp;
    return static_cast< Data&& >( temp );
    }

    Data &&Func2( Data &&d )
    {
    return static_cast< Data&& >( d );
    }

    Func() allocates temp into its stack. The memory address is assumed to be
    0x12FF00. Left value reference is converted into right value reference.
    Before exiting Func(), Destructor is called, but memory address: 0x12FF00
    stays in stack.

    Func2() is called more than two times while right value reference forwards
    the same memory address: 0x12FF00. After returning back to main(), Copy
    Constructor is called and copies all data members into variable x as
    main()'s stack. Then all the data members in memory address: 0x12FF00 is
    removed from the Func()'s stack.

    Please confirm if my code is correct.

    > > int main()

    >
    > > {

    >
    > > Data x = Func2( Func2( Func() ) );

    >
    > >

    >
    > > return 0;

    >
    > > }

    >
    > >

    >
    >
    >
    > Func() is an rvalue expression because you return an object by value.
    >
    > Func2 takes a mutable lvalue reference.
    >
    > This is what's wrong with your program.
    Nephi Immortal, Feb 2, 2013
    #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. Mirek Fidler
    Replies:
    8
    Views:
    548
    Mirek Fidler
    Jul 3, 2003
  2. Hallvard B Furuseth

    Testing nonstandard assumption

    Hallvard B Furuseth, Jun 29, 2003, in forum: C Programming
    Replies:
    1
    Views:
    865
    Hallvard B Furuseth
    Jun 29, 2003
  3. Christian Seberino
    Replies:
    0
    Views:
    338
    Christian Seberino
    Oct 21, 2003
  4. Paul Rubin

    nonstandard XML character entities?

    Paul Rubin, Apr 14, 2007, in forum: Python
    Replies:
    4
    Views:
    463
    Paul Rubin
    Apr 14, 2007
  5. churchy

    <OT?> nonstandard use

    churchy, Sep 21, 2011, in forum: VHDL
    Replies:
    2
    Views:
    929
    Jezmo
    Sep 24, 2011
Loading...

Share This Page