943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2706
  • C++ RSS
Jan 3rd, 2009
0

for loop as a macros

Expand Post »
could somebody tell wats wrong with this
#define FOR(x,n)for(typeof(x);x<n;x++)
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
aryansmit3754 is offline Offline
4 posts
since Jan 2009
Jan 3rd, 2009
0

Re: for loop as a macros

Just don't use macros for loops.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 3rd, 2009
0

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 3rd, 2009
0

Re: for loop as a macros

Apart from anything else this useless macros must be
c++ Syntax (Toggle Plain Text)
  1. #define FOR(x,n)for(typeof(x)x=0;x<(n);x++)
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 3rd, 2010
0
Re: for loop as a macros
"#define FOR(x,n)for(typeof(x)x=0;x<(n);x++)" won't even work. In order for typeof() to function, x has to already exist, and if x already exists the compiler will complain that you're redeclaring x. EG:
C++ Syntax (Toggle Plain Text)
  1. int x;
  2.  
  3. FOR(x,10)
  4. cout<<"Looping"<<endl;
will expand to "for(int x=0; x<(10);x++", but you already declared x... The closest you might want is "FOR(n) for(int x=0; x<(n); x++)" but I fail to see much point. If you're going to macro a for loop, do so because you're controlling the entire loop for a specific set. Such as:
C++ Syntax (Toggle Plain Text)
  1. #define ITERATE_DEVICE_LIST for(int i=0; i < NUM_DEVICES; i++)
At least this ensures that indexing errors need only be fixed in the macro and prevents stray typos from causing problems in the future.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bobpaul is offline Offline
1 posts
since Jun 2010
Jun 3rd, 2010
0
Re: for loop as a macros
Why would anyone do this? Ouch my eyes. Use either std::for_each, BOOST_FOR_EACH,
or even manually.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Operator Overloading
Next Thread in C++ Forum Timeline: C++ number of days between two dates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC