【发布时间】:2021-05-02 00:44:57
【问题描述】:
我正在尝试用 C++98 实现 std::vector。
而且,我提到了https://www.cplusplus.com/reference/vector/vector/vector/
因此,在构造函数中,我对向量进行了如下编码。
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type())
{
...
}
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type())
{
...
}
但是,当我在 main 中测试该向量时,它并没有按我的意愿工作。
int main(void)
{
vector<int> vec(3, 100);
}
我想调用explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()),但是调用了带有迭代器的构造函数。
所以,我的问题是
-
为什么会调用带有迭代器的构造函数?
这是因为'explicit'吗? -
我应该在 main() 中使用 'size_t' 来调用带有 'val' 的构造函数吗?
或者,有什么方法可以检查迭代器吗?
很抱歉打扰你,但我真的不知道为什么会这样......
【问题讨论】:
-
cppreference's page 上提到了更多细节。具体来说,“如果 InputIt 满足 LegacyInputIterator,则此重载仅参与重载解析,以避免与重载 (3) 产生歧义。”不知道是用什么机制来产生这种效果的。
标签: c++