如forward_list中,
template< class... Args >
void emplace_front( Args&&... args );
如果这样设计,就意味着相应的辅助函数也要设计成可变参数模板,在VS中,它的确也是把相关的函数也设计成可变参数的,如:
template<class... _Valty>
iterator emplace_after(const_iterator _Where, _Valty&&... _Val)
{ // insert element at _Where
_Insert_after(_Where, _STD forward<_Valty>(_Val)...);
return (_Make_iter(++_Where));
}
template<class... _Valty>
void _Insert_after(const_iterator _Where,
_Valty&&... _Val)
但是在使用的时候,当时我的理解就是,使用
flist.emplace_front(it2, 22,33,44);
这样的方式。当然会报错,我就想问问,emplace为什么要用可变参数模板?
If I remember correctly, emplace constructs an object directly using parameters at the selected position. It is also reasonable to use variable parameters, just like new with variable parameters and smart pointer constructor.
std::forward_list< std::pair<int, char> > list;
You can call emplace_front(it, 1, 'x');
std::forward_list<int> list;
can call emplace_front(it, 1);
emplace is constructed in-place. In-place construction means that the API caller does not put the object in, but puts the construction object parameters in. Of course, you can only use variable parameter templates.