Retrieving refusal from repeating a template iterator
I need to get a link to a link iterator. However, my compiler is choking on this code:
template <typename InputIterator> size_t iLongestBegin(InputIterator first, InputIterator last)
{
typedef typename std::iterator_traits<InputIterator>::reference SequenceT;
//Problem is next line
typedef typename std::iterator_traits<typename SequenceT::iterator>::reference T;
for(size_t idx; idx < first->length(); idx++)
{
T curChar = (*first)[idx];
for (InputIterator cur = first; cur != last; cur++)
{
if (cur->length() < idx)
return idx;
if (_tolower(cur->at(idx)) != _tolower(curChar))
return idx;
}
}
return first->length();
}
Any ideas on how to fix this? Error
error C2825: 'SequenceT': must be a class or namespace when followed by '::'
Thanks! Billy3
a source to share
Actually, I just decided :)
The problem is that SequenceT is a reference and not a type. Since you cannot normally use a reference type address, the compiler will not generate iterators for it. I need to use value_type instead of reference:
template <typename InputIterator> size_t iLongestBegin(InputIterator first, InputIterator last)
{
typedef typename std::iterator_traits<InputIterator>::reference SequenceT;
typedef typename std::iterator_traits<std::iterator_traits<InputIterator>::value_type::iterator>::reference T;
for(size_t idx; idx < first->length(); idx++)
{
typename T curChar = (*first)[idx];
for (InputIterator cur = first; cur != last; cur++)
{
if (cur->length() < idx)
return idx;
if (_tolower(cur->at(idx)) != _tolower(curChar))
return idx;
}
}
return first->length();
}
a source to share
You need to write typename SequenceT::iterator
instead SequenceT::iterator
. This is because it SequenceT
is a type derived from your template parameters ("dependent type" in standard linguistic), and iterator
is a nested type in SequenceT
, not a function or variable. When both of these things are true, the compiler cannot figure out what you mean and it must be said that SequenceT::iterator
it is type c typename
.
a source to share
The following compilations with g ++ 4.4.0:
#include <iterator>
using namespace std;
template <typename InputIterator> size_t iLongestBegin(InputIterator first, InputIterator last) {
typedef typename std::iterator_traits<InputIterator>::reference SequenceT;
typedef typename std::iterator_traits<typename SequenceT::iterator>::reference T;
for(size_t idx; idx < first->length(); idx++)
{
T curChar = (*first)[idx];
for (InputIterator cur = first; cur != last; cur++)
{
if (cur->length() < idx)
return idx;
if (_tolower(cur->at(idx)) != _tolower(curChar))
return idx;
}
}
return first->length();
}