hi, all,
the initializer_list in c++11 does not support the inner objects to be moved, so in some scenarios the performance will be significant impacted. say, an event class, which stores several std::function objects, and execute them later. the prototype is
template
class event
{
private:
std::vector> v;
public:
event(std::initializer_list>);
void bind(std::function&&);
};
When calling the constructor of event with a set of lambda expressions, the std::function objects will be copied instead of moved, and cause performance impact. Though pointer can resolve the issue, the syntax is not friendly, and the user needs to delete the instances manually.
So I proposal a movable_initializer_list, it accepts two ways to construct, with an initializer_list or initializer_list<T>, the prototype is,
template <typename T>
class movable_initializer_list
{
private:
std::vector<T> v;
public:
movable_initializer_list(std::initializer_list);
movable_initializer_list(std::initializer_list<T>);
// other functions same as initializer_list
};
The implementation of T* overloads is something like
movable_initializer_list x)
{
for(auto it : x)
{
assert((*it) != nullptr);
v.push_back(std::move(**it));
delete *it;
}
}
While the implementation of T overloads is something like
moveable_initializer_listhttps://github.com/Hzj-jie/osi/blob/master/formation/movable_initializer_lis..., and the test is at https://github.com/Hzj-jie/osi/blob/master/utt_cases/formation/movable_initi....
Please feel free to give me some suggestions about this proposal.
Thank you in advanced.
.Hzj_jie