Nonstandard Extension Used?

N

Nephi Immortal

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;
}
 
A

Andrey Tarasevich

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.
 
S

SG

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.
 
N

Nephi Immortal

Am 01.02.2013 23:44, schrieb Nephi Immortal:




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







You tried to initialize a mutable lvalue reference with an rvalue

expression. This is not allowed according to the ISO C++ standard.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top