I am trying to do the following optimization in my container library:
- when pasting an element with a link to lvalue, copy it to internal memory;
- but when you insert an element with a link to rvalue, move it if it is supported.
Optimization should be useful, for example. if the type of the element with the content is similar to std::vectorwhere moving, if possible, will give significant acceleration.
However, so far I have not been able to develop any working scheme for this. My container is quite complex, so I can’t just duplicate the code insert()several times: it is big. I want to save all the "real" code in some kind of internal helper, say do_insert()(maybe a template), and various insert()-like functions will just call it with different arguments.
My best code for this (prototype, of course, without doing anything real):
#include <iostream>
#include <utility>
struct element
{
element () { };
element (element&&) { std::cerr << "moving\n"; }
};
struct container
{
void insert (const element& value)
{ do_insert (value); }
void insert (element&& value)
{ do_insert (std::move (value)); }
private:
template <typename Arg>
void do_insert (Arg arg)
{ element x (arg); }
};
int
main ()
{
{
container c;
element x;
c.insert (x);
}
{
container c;
c.insert (element ());
}
}
However, this does not work, at least with GCC 4.4 and 4.5: it never prints a "move" to stderr. Or what can I not achieve, and why do emplace()similar functions exist in the first place?
source
share