【发布时间】:2019-10-31 15:20:32
【问题描述】:
我正在写一个类似stl vector的类模板,两个构造函数是这样的:
template<class T>
vector<T>::vector(size_t count, const T&value) :bg(new T[count]),
ed(bg + count), cap(ed) {
for (auto it = bg; it != ed; ++it)
*it = value;
}//bg ed cap are all T*
template<class T>
template<class Input>
vector<T>::vector(Input first, Input second) : bg(new T[second - first]),
ed(bg + (second - first)), cap(ed) {
memcpy(bg, (void*)first, sizeof(T)*(second - first));
}
如果我这样做
vector<int>v(2,0)
编译器给我错误,似乎程序使用第二个构造函数而不是第一个。 谁能解释为什么? stl 向量说
这个重载只参与重载决议,如果 InputIt 满足 LegacyInputIterator,以避免与重载产生歧义 (3)。
那么我怎样才能改变我的代码来避免这种情况呢?提前致谢。
【问题讨论】:
标签: c++ class-template