I have code like this:
template<class ListItem>
static void printList(QList<ListItem>* list)
{
for (auto i = list->size() - 1, j = -1; i >= 0; --i) {
std::cout << i << ", " << j << ": " << list->at(i) << std::endl;
}
}
When I compile it with g++ 6.2.1 I get the following compiler output:
test.cpp: In function ‘void printList(QList<T>*)’:
test.cpp:10:7: error: inconsistent deduction for ‘auto’: ‘auto’ and then ‘int’
for (auto i = list->size() - 1, j = -1; i >= 0; --i) {
^~~~
I'd understand this, if variables had different types like auto i = 0.0, j = 0;
, but in this case list is a pointer to QList and its size() method returns int
, -1
on its own should be int
, too. The error message is a bit strange as well.
Variables i
and j
are only needed in this loop and I'd like to declare them as loop parameters. It's not hard to type int
instead of auto, but I'd like to know: is auto
not supposed to be used for declaring multiple variables in one go, or I am missing something here and it really is erroneous code, or maybe it is the compiler's bug?
P.S. Looks like using a template function is the critical part here, factoring the loop out of the template does not produce errors. So, more like a bug in the compiler?