DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   for loop as a macros (http://www.daniweb.com/forums/thread165776.html)

aryansmit3754 Jan 3rd, 2009 3:24 am
for loop as a macros
 
could somebody tell wats wrong with this
#define FOR(x,n)for(typeof(x);x<n;x++)

MosaicFuneral Jan 3rd, 2009 3:51 am
Re: for loop as a macros
 
Just don't use macros for loops.

grumpier Jan 3rd, 2009 3:53 am
Re: for loop as a macros
 
Firstly, there is no space between the closing bracket at the end of FOR(x,n) and preceding the rest. That's a stylistic concern, but badly hurts readability.

Second, and more significantly, typeof() is not a standard function or operator in C++. It is an extension specific to g++ (GNU c++) in which typeof(x) expands to be type of x and will trigger a compiler error with every compiler that does not support that extension. Within a for loop (assuming you're using g++), if x is an int, then
FOR(x,n)
would expand to
for (int; x < n; x++)
which is invalid code.

ArkM Jan 3rd, 2009 4:15 am
Re: for loop as a macros
 
Apart from anything else this useless macros must be
#define FOR(x,n)for(typeof(x)x=0;x<(n);x++)


All times are GMT -4. The time now is 8:24 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC