我有一个STL样列表容器定义了以下两个功能:错误模板函数被调用
// copy the specified VALUE some COUNT number of times and insert copies
// right before POS.
Iterator insert(Iterator pos, size_type count, const value_type
&value);
// copy the values from [FIRST, LAST) from the specified Iterators and
// place before POS.
template<class InputIt>
Iterator insert(Iterator pos, InputIt first, InputIt last);
然后我试着用一些任意的代码来测试我的功能实现:
std::list<int> stlList = { 1, 2, 3, 4, 5 };
MyList<int> intList;
intList.insert(intList.begin(), 5, 0); // expected call to first insert
intList.insert(intList.begin(), stlList.begin(), stlList.end()); // expected call to second insert
但是,对于他们来说,这似乎是第二个函数被调用。我发现模糊不清,因为这两个函数都有三个参数,我看到编译器可能会调用错误的函数。但我不确定我错过了什么。我一直以STL为基础开发自己的功能,并且据我所知,他们以几乎相同的方式定义它们(STL's List Insert)。
什么是'size_type'定义为? – NathanOliver
我认为你的'size_type'是无符号的,所以模板方法是完全匹配的。你可以调用'intList.insert(intList.begin(),5u,0)' – Jarod42
注意关于'std :: list :: insert'的重载(4)(带有两个迭代器的那个):“这个过载只有当InputIt符合InputIterator的条件时才会参与重载解析,以避免过载带来的不确定性(3)。“ –