I remember having read somewhere that VisualC++ does not inline a funtion as soon as a loop is in there or an if statement.
But i cant find the documentation about that anymore, anybody knows these rules or where to find them?
Printable View
I remember having read somewhere that VisualC++ does not inline a funtion as soon as a loop is in there or an if statement.
But i cant find the documentation about that anymore, anybody knows these rules or where to find them?
That's not true.
It inlines this just fine:
Code:#include <iostream>
void test()
{
for (int i = 0; i < 10; i++)
std::cout << "Hello World!\n";
}
int main ()
{
test();
}
No doubt the decision as to whether to inline or not depends on many factors.
To mention a couple which Elysia's example does not show:
- a loop where the number of iterations is not known at compile time might not be inlined.
- a loop body consisting of many statements might not be inlined.
Inlining works best when the cost of calling the function becomes significant in the total amount of work done. So a simple function performing an assignment would be ideal. Inlining an entire sort function is unlikely to make much of a difference (except to bloat the code).
I don't think it's a simple rule as to whether to inline or not. The following is still inlined:
Compilers are probably smarter than this.Code:#include <iostream>
void test(int n)
{
for (int i = 0; i < n; i++)
std::cout << "Hello World!\n";
}
int main ()
{
int n;
std::cout << "Enter num loops: ";
std::cin >> n;
test(n);
}
it seems the documentation I was looking for is outdated now and that VisualC++ has cost/benefit analyzing code now that decides if something is inlined.