Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Nonstandard Extension Used? (http://www.velocityreviews.com/forums/t957200-nonstandard-extension-used.html)

Nephi Immortal 02-01-2013 10:44 PM

Nonstandard Extension Used?
 
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;
}


Andrey Tarasevich 02-01-2013 11:27 PM

Re: Nonstandard Extension Used?
 
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


SG 02-01-2013 11:28 PM

Re: Nonstandard Extension Used?
 
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.


Nephi Immortal 02-02-2013 02:56 AM

Re: Nonstandard Extension Used?
 
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.




All times are GMT. The time now is 05:40 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.