I'm trying to overload a certain function so only iterators of contiguous containers (which are std::vector::iterator ,std::array::iterator ,and built-in arrays iterators == raw pointers) can be a valid argument.
我正在尝试重载某个函数,所以只有连续容器的迭代器(std :: vector :: iterator,std :: array :: iterator和内置数组迭代器==原始指针)才能成为有效参数。
for some reason my functions fail to compile for vector and std::array :
由于某种原因,我的函数无法编译vector和std :: array:
functions:
template <class T,size_t N>
void catchIterator(typename std::array<T, N>::iterator it) {
//do somthing
}
template <class T>
void catchIterator(typename std::vector<T>::iterator it) {
//do somthing
}
example of use :
使用示例:
std::array<int, 10> arr;
auto it = arr.begin();
catchIterator(it);
std::vector<int> vec;
auto it0 = vec.begin();
catchIterator(it0);
Errors :
Error (active) no instance of overloaded function "catchIterator" matches the argument list
Error (active) no instance of overloaded function "catchIterator" matches the argument list
Error C2783 'void catchIterator(std::array<_Ty,_Size>::iterator)': could not deduce template argument for 'T'
Error C2783 'void catchIterator(std::array<_Ty,_Size>::iterator)': could not deduce template argument for 'N'
Error C2672 'catchIterator': no matching overloaded function found
I'm using VC++ with Visual studio 2015 RTM.
我在Visual Studio 2015 RTM中使用VC ++。
The errors are pretty self-explanatory, but I wonder if the compiler couldn't really deduce T and N from it and it0, after all, it is part of it/it0 type..
错误是非常明显的,但我想知道编译器是否真的不能从它和it0中推断出T和N,毕竟它是它/ it0类型的一部分..
how can make it work?
怎么能让它起作用?
Edit:
I'll go with @ForEveR suggestion and pass the container+iterator/index as arguments instead. thanks!
编辑:我将使用@ForEveR建议并将容器+迭代器/索引作为参数传递。谢谢!
1 个解决方案
#1
4
Your functions cannot work, since compiler cannot deduce T
and N
from iterator, that you send to function. You can use std::enable_if
.
你的函数不能工作,因为编译器不能从迭代器中推导出你发送给函数的T和N.您可以使用std :: enable_if。
template <class Iterator>
typename std::enable_if<std::is_same<Iterator, typename std::array<typename std::iterator_traits<Iterator>::value_type, 1>::iterator>::value, void>::type
catchIterator(Iterator it) {
//do somthing
}
template <class Iterator>
typename std::enable_if<std::is_same<Iterator, typename std::vector<typename std::iterator_traits<Iterator>::value_type>::iterator>::value, void>::type
catchIterator(Iterator it) {
//do somthing
}
But there is actually a problem if vector::iterator
and array::iterator
are of the same type (on gcc for example) - code will not compile, since compiler will not know which function it should use.
但是如果vector :: iterator和array :: iterator属于同一类型(例如在gcc上)实际上存在问题 - 代码将无法编译,因为编译器不知道它应该使用哪个函数。
The best way is to just pass container as first argument and iterator as second.
最好的方法是将容器作为第一个参数传递,将迭代器作为第二个参数传递。
template <typename T, size_t N>
void catchIterator(const std::array<T, N> &, typename std::array<T, N>::iterator it) {
//do somthing
}
template <typename T>
void catchIterator(const std::vector<T> &, typename std::vector<T>::iterator) {
//do somthing
}
#1
4
Your functions cannot work, since compiler cannot deduce T
and N
from iterator, that you send to function. You can use std::enable_if
.
你的函数不能工作,因为编译器不能从迭代器中推导出你发送给函数的T和N.您可以使用std :: enable_if。
template <class Iterator>
typename std::enable_if<std::is_same<Iterator, typename std::array<typename std::iterator_traits<Iterator>::value_type, 1>::iterator>::value, void>::type
catchIterator(Iterator it) {
//do somthing
}
template <class Iterator>
typename std::enable_if<std::is_same<Iterator, typename std::vector<typename std::iterator_traits<Iterator>::value_type>::iterator>::value, void>::type
catchIterator(Iterator it) {
//do somthing
}
But there is actually a problem if vector::iterator
and array::iterator
are of the same type (on gcc for example) - code will not compile, since compiler will not know which function it should use.
但是如果vector :: iterator和array :: iterator属于同一类型(例如在gcc上)实际上存在问题 - 代码将无法编译,因为编译器不知道它应该使用哪个函数。
The best way is to just pass container as first argument and iterator as second.
最好的方法是将容器作为第一个参数传递,将迭代器作为第二个参数传递。
template <typename T, size_t N>
void catchIterator(const std::array<T, N> &, typename std::array<T, N>::iterator it) {
//do somthing
}
template <typename T>
void catchIterator(const std::vector<T> &, typename std::vector<T>::iterator) {
//do somthing
}