Jump to content

  • Log In with Google      Sign In   
  • Create Account

Macros vs inline functions.


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1   Members   -  Reputation: 303

Like
0Likes
Like

Posted 24 February 2014 - 03:33 PM

Do you think that macros have a place in C++ programming, or are they completely substitutable by inline functions?


Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

#2   Crossbones+   -  Reputation: 5832

Like
6Likes
Like

Posted 24 February 2014 - 03:49 PM

*
POPULAR

Don't use macros for inline functions.

 

#define is still useful in C++ for:

 

1) Compile time switches via #if/#ifdef etc.

2) Token pasting and stringization operators ##, #. In C you can use those for template-style stuff but you would use actual templates for that in C++. They are sometimes used in C++ for generating class names for unit test frameworks etc.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3   Members   -  Reputation: 1455

Like
1Likes
Like

Posted 24 February 2014 - 08:56 PM


2) Token pasting and stringization operators ##, #. In C you can use those for template-style stuff but you would use actual templates for that in C++. They are sometimes used in C++ for generating class names for unit test frameworks etc.

 

...and for Assert messages.



#4   Crossbones+   -  Reputation: 9180

Like
2Likes
Like

Posted 24 February 2014 - 09:01 PM

... and for being dumb.

#define true false
#define GetMessage GetMessageW

gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=


#5   Crossbones+   -  Reputation: 16040

Like
1Likes
Like

Posted 24 February 2014 - 10:58 PM

... and for being dumb.
 

#define true false
#define GetMessage GetMessageW


#define BIRD WORD

Edited by SeanMiddleditch, 25 February 2014 - 02:03 AM.

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!


#6   Members   -  Reputation: 2389

Like
3Likes
Like

Posted 25 February 2014 - 12:45 AM

Macros are the last resort tool to reduce code duplication. Functions are always preferable when possible, but sometimes they aren't.

For example, if you want a bunch of template specializations for primitive types that are structurally identical, you can use a macro.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS