UnTraDe
UnTraDe

Reputation: 3867

Is inline equals #define function?

Does inline int GetNumber(int i) { return mNumbers[i]; }; equals to: #define GetNumber(i) mNumbers[i] from machine instruction perspective?

mNumbers defined this way: std::vector<int> mNumbers.

Upvotes: 0

Views: 106

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409166

No. Preprocessor macros are replaced like a query-replace in a text editor, and before the code is passed to the compiler proper. Inline function may have their code inserted in place on the call-site. While they may seem similar, it's quite different.

Upvotes: 2

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145214

No.

inline has one guaranteed effect: it allows you to define the same function (identically) in more than one translation unit, and then requires the function to be defined in every translation unit where it's used.

inline also serves as a hint that calls of the function should be inlined at the machine code level, but

  • that hint can be freely ignored, and in case of a recursive function logically must be ignored for some calls,

  • machine code call inlining is not the same as macro expansion, in particular the macro doesn't respect scopes.

Perhaps the most important advice I can give you here is to stop fretting about micro-optimization. It's premature. And premature optimization is evil.

Upvotes: 4

Related Questions