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 » Why there is no Index::Add(T&&)?
Why there is no Index::Add(T&&)? [message #49094] Sun, 17 December 2017 05:42 Go to next message
busiek is currently offline  busiek
Messages: 43
Registered: February 2011
Location: Poland
Member
I need to store large objects as keys in Index, thus I prefer to pass ownership of a key to the container. I had to create my own container:
template <class T>
class HeavyIndex : public Index<T>, public MoveableAndDeepCopyOption<HeavyIndex<T>>
{
	typedef Index<T> B;
public:
	T& Add(T&& x, unsigned _hash)
	{
		T& t = B::key.Add(pick(x));
		B::hash.Add(_hash);
		return t;
	}
	T& Add(T&& x) { return Add(pick(x), B::hashfn(x)); }
	int FindAdd(T&& x, unsigned _hash)
	{
		int i = B::Find(x, _hash);
		if(i >= 0) return i;
		i = B::key.GetCount();
		Add(pick(x), _hash);
		return i;
	}
	int FindAdd(T&& x) { return FindAdd(pick(x), B::hashfn(x)); }
};


However, probably a single version of Add() method can be created using a solution with std::forward similarly as in Fixes to Array::Create & Vector::Create.
Re: Why there is no Index::Add(T&&)? [message #49099 is a reply to message #49094] Sun, 17 December 2017 22:09 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 817
Registered: December 2006
Experienced Contributor
busiek wrote on Sat, 16 December 2017 23:42
However, probably a single version of Add() method can be created using a solution with std::forward similarly as in Fixes to Array::Create & Vector::Create.

In order to make std::forward work method Add has to be a template method.
template<typename A>
A& Add(A&& x, unsigned _hash);

Using T&& with a method of a template class won't create a forwarding reference.


Regards,
Novo
Re: Why there is no Index::Add(T&&)? [message #49100 is a reply to message #49099] Sun, 17 December 2017 22:19 Go to previous messageGo to next message
busiek is currently offline  busiek
Messages: 43
Registered: February 2011
Location: Poland
Member
Novo wrote on Sun, 17 December 2017 22:09

Using T&& with a method of a template class won't create a forwarding reference.

Right. I shouldn't write messages in the middle of the night. Rolling Eyes
Re: Why there is no Index::Add(T&&)? [message #49335 is a reply to message #49100] Sun, 28 January 2018 19:14 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11892
Registered: November 2005
Ultimate Member
Now in trunk, r-value variants added to all relevant Index, VectorMap, ArrayMap methods.
Re: Why there is no Index::Add(T&&)? [message #50836 is a reply to message #49335] Mon, 07 January 2019 14:47 Go to previous messageGo to next message
busiek is currently offline  busiek
Messages: 43
Registered: February 2011
Location: Poland
Member
That was great addition. Thanks.

Similar problem is with InVector and relevant containers. Methods like Add, Insert, InsertUpperBound, InsertRange, AppendRange and so on should have T&& variants too. It shouldn't be too much work to add that.
Re: Why there is no Index::Add(T&&)? [message #50840 is a reply to message #50836] Tue, 08 January 2019 09:36 Go to previous message
mirek is currently offline  mirek
Messages: 11892
Registered: November 2005
Ultimate Member
Issue filed, will happen.

Mirek
Previous Topic: GetCsvLine
Next Topic: The right container
Goto Forum:
  


Current Time: Wed Jun 19 19:25:04 CEST 2019

Total time taken to generate the page: 0.00836 seconds