inline + gcc optimization level 0

Hello,

does anybody know if the compiler respects the keyword inline at gcc optimization level 0?


Best regards,

Seth
You could give it a test:
1
2
3
4
5
6
7
8
//header.hpp
//note: no include guards to allow multiple inclusion
#include<iostream>

inline void func()
{
    std::cout << "Hello, inline!" << std::endl;
}
1
2
3
4
5
6
7
8
9
10
11
//main.cpp
#innclude "header.hpp"
#innclude "header.hpp"

void func2();

int main()
{
    func();
    func2();
}

1
2
3
//secondary.cpp
#include "header.hpp"
void func2(){ func(); }
If it compiles and links, it was inlined.
Last edited on
It does
It depends on what you mean by "respects". GCC does follow all the rules about inline functions even with lowest optimization level. The compiler doesn't have to actually inline an inline function. I'm not sure how GCC will handle this. Does it really matters?

The code that LB has posted will not compile because func() is defined multiple times in the same translation unit. This has nothing to do with inline.
Last edited on
Oh, if you meant actually inlining the code from the function to where it is used, then that IS a good question.

With optimizations disabled, and an inlined function, and considering that inlining is often an optimization even with functions that are not marked as inline, I can only think of one way to test.

1
2
3
4
5
6
7
struct Copyable
{
    Copyable(){std::cout<<"1";}
    Copyable(const Copyable &){std::cout<<"2";}
    Copyable &operator=(const Copyable &){std::cout<<"3";return*this;}
    ~Copyable(){std::cout<<"4";}
};


Try passing a Copyable by value to an inline function and see what happens.
LB wrote:
Try passing a Copyable by value to an inline function and see what happens.

I don't see how this will give any information about whether the function is inlined or not.
Last edited on
With no optimizations:
if it is inlined then the copy constructor will not be called.
if is not inlined then it will make a copy to pass by value.

This is an assumption.
The compiler is not allowed to make such optimizations because that would change the outcome of the program. There are some special rules about temporary objects, so if you pass a temporary object to the function it can optimize so that only one object is created but that could happen even if the function was inlined or not.
@ L B
L B wrote
With no optimizations:
if it is inlined then the copy constructor will not be called.
if is not inlined then it will make a copy to pass by value.


I don't think that an inline function taking an object by value as it's param, will cause the compile not to call the copy constructor of the class! I am afraid to say that your assumption is not true.
Last edited on
closed account (zb0S216C)
I don't know if it helps, but all default constructors, destructors, and operators provided by the compiler are inline.

Wazzak
This is silly.

For inline functions, it isn't all or nothing. Sometimes, it can be inlined and sometimes it can't. The only thing you prove by finding a place where it doesn't inline is that it doesn't inline in that particular case.

At optimization level O, some inlining is done, but the inline keyword is never 'respected' if that means it's used as a cue to inline a function.



Last edited on
> does anybody know if the compiler respects the keyword inline at gcc optimization level 0?

Yes. Unless -f-no-inline is also specified. However, no 'automatic inlining' will be done - only those functions which have the inlne specifier will be considered for inlining.

Invoke g++ with this command line: >g++ -O1 -Q --help=optimizers and it will output a list of optimization options along with the enabled/disabled status of each at that optimization level.

RTFM here: http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Optimize-Options.html#Optimize-Options

Last edited on
Topic archived. No new replies allowed.