Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
Today I am approaching the wonderful community of Code Project, with a question about rewriting functions as macros in C/C++.

I'm currently learning C (and C++), and, as an activity to help me learn more, I'm writing a program that'll take a file, and read it, and then display the data in a way that is useful to the user. The project might be fairly useless, but so far, it has been worth it. (I've been able to learn File I/O, and I took the liberty of learning the basics of pointers, so that I would understand more about how File I/O works in C/C++)

Now, to get to the point of this question:
I wrote a function, that takes a string, and "explodes" it, based on a token (yes, I know there is tokstr, but again, this is for learning). I've been reading about macros lately, and I've gotten mixed-opinions about them.

On one hand, people say "yes, use macros [if you plan on reusing the code]", and on the other, people say "no, macros should be avoided in C++", so I'm sort of trying to figure out, should I invest time in writing a macro to replace this function?

Secondly, I'd like to know (even if I'm not going to write a macro for this function), what should I refrain from doing in a macro? I've also read that you should surround code in your macro(s), with a do/while loop, is there a specific reason?

I do apologize for the long question, but hopefully I'll be able to get some help.

Happy St. Patrick's Day,

Gigabyte Giant
Posted
Comments
Sergey Alexandrovich Kryukov 17-Mar-15 16:42pm    
Why? why?!
—SA
Gigabyte Giant 17-Mar-15 16:43pm    
Why what?
Sergey Alexandrovich Kryukov 17-Mar-15 16:56pm    
If you have a function, why converting it to a macro?
—SA
Gigabyte Giant 17-Mar-15 16:58pm    
I'd like to lay emphasis on the multiple times I said "this is for learning"... I'm trying to learn C/C++, therefor I want to soak up all the knowledge I can about it... I have collected numerous books about the languages, but I still have questions.
Sergey Alexandrovich Kryukov 17-Mar-15 21:42pm    
I really appreciate that. Thank you for this explanation.
Cheers,
—SA

1 solution

Why macros are good?

1. avoid repetitive typing (less risk of mistakes).

Examples: MFC, boost and ATL/WTL use macros for this reason.

2. trick existing code to be compiled with wildly different behavior.

Examples: Mock facilities for testing; swapping out definitions of new and delete for heap debugging.

3. generated code - source code not meant for hand-editing.

Why macros are bad?

1. Adversely impacts debugging.
a. single-step through a macro that expands to a dozen lines.
b. set a break point on a macro definition.

2. Hiding - macros can hide things from you - to make debugging and code review more difficult.

Example: #define true false

3. Trick existing code to be compiled with wildly different behavior.

4. Macros are not confined to a namespace.

Back in the day - before inline functions - a function defined as a macro was a way to have inline code instead of a plain function call. With the __inline keyword, this use of a macro is obsolete.

Summary

Use a macro only if absolutely needed and only if there is no other mechanism. Instead, use const, enum, and typedef for types and constants / literals.

Your interest in re-writing a function as a macro has much value as a learning exercise but no more. You will also find a few surprises once you start defining macros using other macros.

After you're done learning macros, you can eventually play with C++ templates - which are more more powerful than macros - but can be confined to a namespace.

Good luck.
 
Share this answer
 
Comments
Gigabyte Giant 17-Mar-15 14:02pm    
Definitely interesting! +5 stars.
I'll refrain from converting future functions to macros, however, out of curiosity, what should I avoid doing when converting a function to a macro? How much will I need to change the syntax? Can I still use loops?
[no name] 17-Mar-15 15:15pm    
Mutli-line macros require a backslash at the end of each line - except the last line so ...

void something_useful(const char *something, int x, int y)
{
int sum = x + y;
printf("Say %s sum is %d\n", something, sum);
}

... becomes ...

#define SAY(something, x, y) \
{ \
int sum = x + y; \
printf("Say %s sum is %d\n", something, sum); \
}

Notice that you also lose the compile time type safety in this example.
Gigabyte Giant 17-Mar-15 18:54pm    
I see! Thanks for your help, accepting as solution!
Sergey Alexandrovich Kryukov 17-Mar-15 16:42pm    
Nice explanation, a 5.
—SA
[no name] 17-Mar-15 17:31pm    
+5. Well explained.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900