views:

91

answers:

3

Are operators always inlined?

struct foo {
    void operator ()() {
        // Do tons of work.
    }
};

int main() {
    foo f;
    f();
}
+1  A: 

No, they are not. The compiler is completely free to ignore all and any requests that a function be inlined. What it can't ignore is that it must give them internal linkage, so a header containing them can be included in multiple translation units.

anon
It is not true that inline functions must have internal linkage. By default `inline` functions don't get internal linkage. They do get an exemption from ODR, and the program has on obligation to defined them in every TU where they are used but they are still the same function (e.g. address of, address of static data item in an inline function must always have the same value).
Charles Bailey
+2  A: 

An operator is a normal function just as any other function.

IanH
But an operator (or any other function) with a body defined in a class is an explicit request that the function be inlined - which the compiler can ignore.
anon
@Neil: I'm not sure what your point is. "...just as any other function" nicely covered that.
sbi
@sbi The answer does not mention inlining.
anon
@Neil: So? It answers the question.
sbi
@sbi No, it doesn't. It requires you to know the rules for "normal functions" (whatever they may be) which the operator (and me) don't know.
anon
@Neil: I guess we just have to agree to disagree then. `<shrug>` For me it does.
sbi
+1  A: 

The compiler is the unprecedented and (officially) unpredictable lord of inlining decisions. Good compilers will provide some guidance in the documentation about their implementations behaviour. The more complicated the code the less likely it is to be inlined, you can find some examples of what does/doesn't tend to inline on the Wikipedia.

"Do tons of work" on it's own suggests that your intended operator is too complicated for most compilers to inline.

Microsoft's Visual C++ compiler can be made to generate warnings, when it decides to inline a function that wasn't marked inline and when it doesn't inline one that was marked inline. I like it for getting a feel for what it can inline.

TerryP