Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » How to fix this strong behaviore of MSC compiler & temporary value ?
How to fix this strong behaviore of MSC compiler & temporary value ? [message #44615] Fri, 24 April 2015 12:56 Go to next message
omari is currently offline  omari
Messages: 243
Registered: March 2010
Experienced Member
Hello:

the code below does not work as expected when compiled with MSC compiler, tested with MSC10/WinXP and MSC12/Win7:


#include <Core/Core.h>

using namespace Upp;

class A 
{
public:
	int x;
	A():x(0)             	{}

	A( A rval_ a) 			{ LOG("from A");x = a.x;}
	A(String const &s) 		{ LOG("from String");}
	operator String() 		{ LOG("to String"); return ""; }
};

A GetA()
{
	A a;
	a.x = 6;
	return a;
}

CONSOLE_APP_MAIN
{
	A a = GetA();	
	DUMP(a.x);
}
 


the output of this test case is :


from A
to String
from String
a.x = 1310556





regards
omari.

[Updated on: Fri, 24 April 2015 13:24]

Report message to a moderator

Re: How to fix this strong behaviore of MSC compiler & temporary value ? [message #44616 is a reply to message #44615] Sat, 25 April 2015 10:07 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 227
Registered: July 2011
Location: Bangalore
Experienced Member
Hello Omari

Try this,
Declare first. Then use.
A a ;

CONSOLE_APP_MAIN
{
	A a ;   // this is changed
	a = GetA();	
	DUMP(a.x);
}


A a = GetA();
Declaration and assignment on same line does not work particularly function return value.
I don't know why it is this way.

I experienced this behavior with some other functions.


Warm Regards

Deepak

[Updated on: Sat, 25 April 2015 18:49]

Report message to a moderator

Re: How to fix this strong behaviore of MSC compiler & temporary value ? [message #45268 is a reply to message #44616] Mon, 26 October 2015 12:51 Go to previous message
omari is currently offline  omari
Messages: 243
Registered: March 2010
Experienced Member
I think the best solution is to never use "const &", but use rval_

class A 
{
public:
	int x;
	A():x(0)             	{}

	A( A rval_ a) 			{ LOG("from A");x = a.x;}
	A(String rval_ s) 		{ LOG("from String");}
	operator String() 		{ LOG("to String"); return ""; }
};


work as expected.


regards
omari.
Previous Topic: Initialization for Buffer<T>
Next Topic: mingw/gdb troubles
Goto Forum:
  


Current Time: Fri Jan 22 16:10:30 CET 2021

Total time taken to generate the page: 0.01897 seconds