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  |
busiek
Messages: 70 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   |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
busiek wrote on Sat, 16 December 2017 23:42However, 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
|
|
|
|
|
|
|
Goto Forum:
Current Time: Tue Apr 29 01:47:11 CEST 2025
Total time taken to generate the page: 0.01901 seconds
|