My code looks like this
#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
...
}
The code looks okay to me, but not to my compiler! Mercilessly, I am given the following errors:
code.cpp: In function ‘int main()’:
code.cpp:95:9: error: specialization of ‘std::vector::iterator’ after instantiation
code.cpp:95:9: error: expected primary-expression before ‘typeof’
code.cpp:95:9: error: expected ‘;’ before ‘typeof’
code.cpp:95:9: error: name lookup of ‘j’ changed for ISO ‘for’ scoping
code.cpp:95:9: note: (if you use ‘-fpermissive’ G++ will accept your
code)code.cpp:95:9: error: no match for ‘operator!=’ in ‘j !=
numbers.std::vector<_Tp, _Alloc>::end with _Tp = long int, _Alloc =
std::allocator, std::vector<_Tp, _Alloc>::iterator =
__gnu_cxx::__normal_iterator >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer =
long int*’
What am I missing here?
You have a missing parenthesis in the macro.
Try fixing that and see what errors you get.
Minor note 1: When iterating, use
++iinstead ofi++as the former can often faster for iterators.Minor note 2:
typeofis a GCC specific extension. Your code will not compile on other compilers.