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   

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   Members   

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   

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   Moderators   

9747
Like
2Likes
Like

Posted 24 February 2014 - 09:01 PM

... and for being dumb.

#define true false
#define GetMessage GetMessageW

zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#5   Members   

16785
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   

2391
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.