开发者

How to get the type of a variable in a macro?

开发者 https://www.devze.com 2023-03-30 06:37 出处:网络
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)

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消