I have a question regarding inline function.
#include <iostream> using namespace std; class A { public: inline void fun() { int i=5; cout<<i<<endl; } }; int main() { A a1; int i = 2; a1.fun(); cout<<i<<endl; return 0; } In above program, when function fun() was called, the compiler should have made copy of this function and insert into main function body because it is inline.
So, I have a question, Why doesn't compiler give an error of variable int i; being re-declared?
inlinekeyword does not guarantee that the function will be inlined. In fact, most compilers ignore it completely for inlining desitions. It has other uses, like ODR, but it no longer has any meaning for actual inlining. At best it's a inlining hint, but I don't think any modern compilers use it for even that any more.inline__force_inlinekeyword that will do exactly what it sounds like. If you are certain that a function should be inlined you can guarantee it this way. I find it unlikely that someone will judge better than the compiler and it will not permit the compiler to choose based on target architecture. In any case, one should always measure the performance difference if__forceinlineis used.