【发布时间】:2018-02-15 05:10:03
【问题描述】:
我想编写函数来添加项目。 addItem 和 addItems 每个都有一个移动变量。后者接受两个输入迭代器。要添加单个项目,我可以使用右值引用重载签名。但是如何重载模板函数以使用移动语义?
void addItem(const shared_ptr<Item>& item, uint score) {
// code that copies the shared_ptr…
}
void addItem(shared_ptr<Item>&& item, uint score) {
// code that moves the shared_ptr…
}
template<typename Iterator>
void addItems(Iterator begin, Iterator end) {
/*
* What to do here to take both move and normal iterators?
* Since I cannot overload by signature I dont know how to
* differentiate between move and non move iterators
*/
}
是否可以为函数使用一个名称并区分输入迭代器?
【问题讨论】:
-
我有,但我不明白如何重载函数以同时接受移动和非移动迭代器。
-
@ManuelSchneid3r:您当前的签名接受 all 迭代器
标签: c++ templates move-semantics