I made a function find_all which returns a vector of position(iterator) of all position of element xr
in a STL container ar
. What`s wrong with this code.
template<typename T1, typename T2 = T1::value_type>
auto find_all(const T1& ar, T2 xr)
{
typedef T1::const_iterator const_iterator;
vector<const_iterator> it;
for (auto it2 = ar.cbegin(); it2 != ar.cend(); ++it2)
if (*it2 == xr)
it.push_back(it2);
return it;
}
The code compiles in Visual C++ 2015, but gives following error with gcc compiler: Error :
prog.cpp:3:38: error: need 'typename' before 'T1::value_type' because 'T1' is a dependent scope
template<typename T1, typename T2 = T1::value_type>
^
prog.cpp: In function 'auto find_all(const T1&, T2)':
prog.cpp:6:11: error: need 'typename' before 'T1::const_iterator' because 'T1' is a dependent scope
typedef T1::const_iterator const_iterator;
^
prog.cpp:7:10: error: 'const_iterator' was not declared in this scope
vector<const_iterator> it;
^
prog.cpp:7:24: error: template argument 1 is invalid
vector<const_iterator> it;
^
prog.cpp:7:24: error: template argument 2 is invalid
prog.cpp:7:28: error: invalid type in declaration before ';' token
vector<const_iterator> it;
^
prog.cpp:10:8: error: request for member 'push_back' in 'it', which is of non-class type 'int'
it.push_back(it2);
^
I want to simply correct it.