我正在试验 std::remove_reference。例如,我可以将元素类型提取为数组,但如何让 remove_reference 与 STL 容器一起使用?例如,我想使用下面的 remove_reference 将迭代器返回到向量的元素:
#include <iostream>
#include <vector>
#include <type_traits>
using std::vector;
using std::cout;
using std::endl;
using std::begin;
using std::end;
using std::remove_reference;
template<typename T>
auto my_end(T& c) -> typename remove_reference<decltype(&c[0])>::type
{
return end(c)-1; //compile error when myend<vector> is instantiated
}
int main()
{
int ia[] = {1,2,3,4,5,6,7,8,10};
vector<int> v(begin(ia), end(ia));
auto my_back1 = *my_end(ia);
cout << my_back1 << endl; //prints 10
auto my_back2 = *my_end(v);
cout << my_back2 << endl; //should print 10
}
实例化时的编译器错误my_end<vector>
是:
cannot convert from 'std::_Vector_iterator<_Myvec>' to 'int *'