小编dro*_*ngo的帖子

使用C++ 11 auto关键字声明两个(或更多)变量

我有这样的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我用g ++ 6.2.1编译它时,我得到以下编译器输出:

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) {
       ^~~~
Run Code Online (Sandbox Code Playgroud)

我理解这一点,如果变量有不同的类型auto i = 0.0, j …

c++ templates auto c++11

25
推荐指数
1
解决办法
1650
查看次数

标签 统计

auto ×1

c++ ×1

c++11 ×1

templates ×1