Pergunta

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.

Foi útil?

Solução 2

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top