我在这里没有正确使用typeof()吗?

Am I not using typeof() correctly here?

本文关键字:typeof 在这里      更新时间:2023-10-16

我的代码看起来像这样

#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
...
typedef vector<long> vl;
vl numbers;
...
tr(numbers, j) { // this is line 95
    ...
}

代码对我来说看起来不错,但对我的编译器来说却不行!无情地,我得到了以下错误:

code.cpp: 在函数 'int main((' 中:

code.cpp:95:9:错误:实例化后"std::vector::迭代器"的专用化

code.cpp:95:9:错误:"typeof"之前的预期主表达式

code.cpp:95:9:错误:在"typeof"之前应为";">

code.cpp:95:9:错误:ISO "for"范围更改了"j"的名称查找

code.cpp:95:9:注意:(如果你使用"-fallowive",G++ 将接受你的 代码(

代码.cpp:95:9:错误:与"J!= "中的"运算符!="不匹配 numbers.std::vector<_Tp, _Alloc>::end with _Tp = long int, _Alloc = std::分配器, std::vector<_Tp, _Alloc>::迭代器 = __gnu_cxx::__normal_iterator>, 键入名称 std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::p ointer = 长内*'

我在这里错过了什么?

宏中缺少括号。

#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
                                      ^
                                 Need ) here

尝试修复它,看看你得到了什么错误。

次要说明 1:迭代时,请使用 ++i 而不是 i++因为前者通常可以更快地用于迭代器。

次要说明 2:typeof是 GCC 特定的扩展。您的代码不会在其他编译器上编译。

我认为你缺少一个'('

#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)