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 gasto   Members   -  Reputation: 261

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.

Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5254

Like
6Likes
Like

Posted 24 February 2014 - 03:49 PM

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 Aliii   Members   -  Reputation: 1445

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 fastcall22   Crossbones+   -  Reputation: 4250

Like
2Likes
Like

Posted 24 February 2014 - 09:01 PM

... and for being dumb.

#define true false
#define GetMessage GetMessageW

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#5 SeanMiddleditch   Members   -  Reputation: 5113

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.


#6 King Mir   Members   -  Reputation: 1952

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