How to get the type of a variable in a macro?
I'm currently using this code for a foreach macro:
#define foreach(T, arr, it) for(T::iterator it = (arr).begin(), itend = (arr).end(); it != itend; ++it)
How can i get rid of the first parameter T
and make the compiler 开发者_运维技巧figure out which type it should copy/paste in the code T::iterator
? It seems rather silly i need to type it myself when it could be figured out from the variable itself.
Since this is pre auto
and decltype
, your best bet is using Eric Niebler's FOREACH
macro.
Or upgrade your compiler to VC 2010 and just use auto
.
If you're using a compiler capable of C++11 you can use auto
:
#define foreach(arr, it) for(auto it = (arr).begin(), itend = (arr).end(); it != itend; ++it)
精彩评论