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 » U++ Library support » U++ Core » A probably simple question about pick semantics
A probably simple question about pick semantics [message #34794] Mon, 12 December 2011 16:58 Go to next message
peek is currently offline  peek
Messages: 13
Registered: March 2010
Promising Member
I wanted to pass a Vector by reference to a template function, but the Vector is lost Sad :

.cpp file

Vector<double> v;
v << 1;

Test(v);


.h file

template <class T>
void Test(T v)
{
	// Nothing done
}


After calling to Test(), Vector v contents disappear.

When debugging you can see than before jumping to Test(), the compiler call to Vector pick constructor:

Vector(pick_ Vector& v)          { Pick(v); }


and before leaving Test(), the compiler calls Vector destructor:

~Vector()  { ....


However if Test is called with a pointer to Vector, v contents remains.

Test(&v)


What could we do to call the templated function without loosing Vector contents?

Thank you Smile
Re: A probably simple question about pick semantics [message #34795 is a reply to message #34794] Mon, 12 December 2011 17:08 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Use a reference parameter: Test(T& v) or Test(const T& v).

Pick semantics kick in on a copy operation. Even without pick semantics using reference would be advised, since otherwise you would be needlessly copying around data when passed to the function.
Re: A probably simple question about pick semantics [message #34915 is a reply to message #34795] Fri, 16 December 2011 22:35 Go to previous messageGo to next message
peek is currently offline  peek
Messages: 13
Registered: March 2010
Promising Member
Hello cbpporter

I have tried it doing it by reference but the problem remains Sad.
template <class T>
void Test(T &v)
{
	// Nothing done
}

In fact I forgot to put the "&" in the sample code.

Is there a way to avoid this copy in a Vector passed by reference to a templated function?
Re: A probably simple question about pick semantics [message #34917 is a reply to message #34915] Fri, 16 December 2011 22:49 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
peek wrote on Fri, 16 December 2011 16:35

Hello cbpporter

I have tried it doing it by reference but the problem remains Sad.
template <class T>
void Test(T &v)
{
	// Nothing done
}

In fact I forgot to put the "&" in the sample code.

Is there a way to avoid this copy in a Vector passed by reference to a templated function?


More complex (read "compilable") example would be needed...

In general, this has to work, because such templates are widespread in the U++ code (read "theide would not run" Smile

Mirek
Re: A probably simple question about pick semantics [message #34918 is a reply to message #34917] Sat, 17 December 2011 00:26 Go to previous message
peek is currently offline  peek
Messages: 13
Registered: March 2010
Promising Member
Sorry... there is no problem.

Thank you all !!
Previous Topic: gdk_pixbuf-2.0 linker error
Next Topic: Small fix for Find in Algo.h
Goto Forum:
  


Current Time: Fri Mar 29 16:10:58 CET 2024

Total time taken to generate the page: 0.01607 seconds