C++ Macro Problem

Started by
13 comments, last by Aardvajk 12 years, 10 months ago

It's practical in nature. I read in a few places that inline isn't guaranteed so I thought macros for these simple operations would be better.
[/quote]
Just because it isn't guaranteed doesn't mean it isn't happening. Such a trivial function will almost certainly be inlined.

As Asrdvajk says, only profiling can indicate if the decision to inline or not is justified. Remember than forcing inlining can cause code bloat, which can slow things down instead of speeding them up.
Advertisement

Others had given the answer.

One more thing, VC can (maybe GCC also can) generate .i files which is expanded by preprocessor, so you can see how the macro is expanded.

But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros.


Just curious, which word is wrong to get down vote?
Voting worth nothing but unreasonable down vote is not friendly.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.


Just curious, which word is wrong to get down vote?
Voting worth nothing but unreasonable down vote is not friendly.


One errant down vote shouldn't be taken too seriously. I would *guess* that it was your last line: "But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros." This seems to support using macros for bizarre, unusual, unreadable, and surprising code. Generally the better advice is to avoid anything "magical" in the first place. However, I think you were just trying to say to use them when the only alternatives are going to be really messy.

BTW, I gave you an upvote since your .i file suggestion is a good one.

One errant down vote shouldn't be taken too seriously. I would *guess* that it was your last line: "But more important, be careful to use macro, only use macro for magic things.


Macro can do magic things for very useful cases, one case is simulating variadic template parameters. Boost library uses it. Boost also used macro extensively for some magic things which the library users will never aware.
Carefully using macro will really get very amazing powerful use. I used macros to make a callback system.
That's I called magic.
Functions and constants are not magic, so don't use macros for them.


BTW, I gave you an upvote since your .i file suggestion is a good one.


Thanks.
I really hope gdnet won't go stackoverflow way, full of up/down vote and a lot of votes are quite bias (GCC fans may down vote any one recommending VC, etc) and subjective. (no argument on this point because it's off topic).

Edited:
Again i don't mind down vote, but please jump out to give the reason, thanks.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Downvotes corrected. No idea what that was about. We are all aware that there are certain things you can only do with macros in C++.

My personal rule of thumb is: If it can be done without a macro, do it the other way, otherwise suck it up or move to a more modern language.

This topic is closed to new replies.

Advertisement