표준 : : remove_reference를 실험하고 있습니다. 예를 들어 배열에 요소 유형을 추출 할 수 있지만 STL 컨테이너에서 작동하도록 remove_reference를 얻는 방법은 무엇입니까? 예를 들어, 나는 remove_reference 아래 사용하여 벡터의 요소에 반복자를 반환 할 :std :: remove_reference를 사용하여 STL 컨테이너의 요소 반복자를 얻으십시오.
이#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 *'
'decltype (& C [0])'를 잘 –
왜 그냥'유형 이름의 T를 보이지 않는 :: iterator'를? 아니면'decltype (c.begin())'해야합니다. –
@KerrekSB 또는 그가 정말로해야한다면'decltype (end (c) -1)'. – pmr