I wrote this code:
#define VECTOR_LOOP_V(X) for (vector<typeof(X)>::iterator it = X.begin(); it != X.end(); it++)
to make it faster to write for loops for vectors but for some reason it does not work, and when I try to compile it it gives me really, really long error message.
test.cpp: In function ‘int main(int, char**)’:
test.cpp:20:5: error: conversion from ‘std::vector<std::basic_string<char> >::iterator {aka __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ to non-scalar type ‘std::v
etc..
It doesn’t work because
typeof(X)yieldsstd::vectortype and so the macro expands to something like:Also, your macro would fail if
Xis constant, in which caseconst_iteratorshould be used. There are tons of other problems, but at any rate, what you were trying to do is something like this:.. and here is some usage example:
Note, however, that
typeofis not a standard C++ (see this Q/A).All in all, you are better off using BOOST_FOREACH (or at least see how it is implemented there) or C++11 range-based for loop.
P.S.: Do not use the STL abbreviation unless you really mean STL and not C++ Standard Library (which you can refer to as, say, stdlib).