I’m trying to implement a function that allows me to make a call like this
// veca is a vector of tuples in my case
columnViewOfTuple<0>(veca);
I implemented such function as follows
template<int N>
struct myfunction {
template<typename T, typename R>
std::vector<R> operator() (T& container)
{
std::vector<R> myvector;
for(typename T::iterator it = container.begin(); it!=container.end(); it++)
myvector.push_back((R)(*it).template get<N>());
return myvector;
}
};
whenever I call myfunction<0>(vec5), where vec5 is some vector of tuples, it says
main.cpp: In function ‘int main()’:
main.cpp:156: error: conflicting declaration ‘myfunction<0> vec5’
main.cpp:155: error: ‘vec5’ has a previous declaration as ‘main()::vec1_t vec5’
Do you guys know how to fix this?
Thanks
The loop should be
Otherwise, the compiler will treat
T::iteratoras a non-type (at parse-time, it does not yet know whatT::iteratoris going to be later!) and will likely parse it as the sole constituent of an expression. Theitthat then follows is nonsense for the compiler so it expects a;before it.typenameis used to tell the compiler that a certain qualified name is intended to denote a type instead of a value (function / static data member / etc).The second issue, which is solved by adding
templateis of similar kind. It tells the compiler thatgetis a template and thus that<Nis not a comparison againstN, but the start of a template argument list.