Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I stop using "inline" altogether? [duplicate]

Since it's entirely up to compiler whether or not it inlines my functions can I just stop using this keyword completely (assuming it will inline everything it can anyway)?

like image 604
NPS Avatar asked Apr 07 '13 12:04

NPS


People also ask

Can we use both embedded and external styling together?

Yes we can use all three of them for the same page. We can even use all three having different properties for a same element just like various diffrent color for same h1 with 3 diffrent methods.

How do I move inline block to center?

Inline block divs can be centered by applying text-align:center to their parent.

How many different ways are there to ensure style rules can be applied to your content 1 internally 2 externally 3 inline?

There are three ways you can use to implement CSS into your HTML: internal, external, and inline styles.

Can we use internal and external CSS?

To add CSS styles to your website, you can use three different ways to insert the CSS. You can Use an “External Stylesheet“, an “Internal Stylesheet“, or in “Inline Style“. The benefit for using each depends on what you are doing with the Style.


2 Answers

You can stop using inline as an optimization technique.

inline is basically useful only when you want the ODR (One Definition Rule) not to apply. In short, you can mark functions as inline and provide their definition directly in one header file, which is then imported by multiple translation units, without the linker complaining about this:

foo.hpp

inline int foo() { return 42; }

foo.cpp

#include "foo.hpp" // <== THE LINKER WOULD COMPLAIN IF foo() WERE NOT inline,
                   //     because its definition is imported also in main.cpp
void bar()
{
    int x = foo();
}

main.cpp

#include "foo.hpp" // <== THE LINKER WOULD COMPLAIN IF foo() WERE NOT inline,
                   //     because its definition is imported also in foo.cpp
int main()
{
    std::cout << foo();
}

The presence of the inline keyword (which, again, does not guarantee that the compiler will perform inlining) ensures that the linker will merge those definitions.

Of course, in order for this merge operation to be meaningful, all the definitions of a function marked as inline which end up being part of several different translation units must be identical. If this is not the case, your program has Undefined Behavior and no diagnostic is required - which means that your compiler/linker is not required to detect this inconsistency and tell you that something is wrong!

Per Paragraph 3.2/4 of the C++11 Standard, in fact:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.

Notice, that you can have the same function marked as inline and literally defined twice in different translation units, and that is fine as long as those definitions are identical:

foo.cpp

inline int foo() // Has the same body in main.cpp, no problem!
{
    return 42;
}

main.cpp

inline int foo() // Has the same body in foo.cpp, no problem!
{
    return 42;
}

int main()
{
    std::cout << foo();
}

However, if the two definitions differ, you will inject UB in your code, as in the following example:

foo.cpp

inline int foo()
{
    return 42;  // <== WATCH OUT! Different body in main.cpp
}

main.cpp

inline int foo()
{
    return -42; // <== WATCH OUT! Different body in foo.cpp
}

int main()
{
    std::cout << foo();
}

This is why you normally mark functions as inline when you provide their definition directly in a commonly #included header file.

Also notice, that class member functions whose definition is directly inlined in the class definitions are automatically marked as inline.

like image 128
Andy Prowl Avatar answered Oct 02 '22 19:10

Andy Prowl


Depends on purpose of using inline.

The common (mis)conception:
inline is just a suggestion which a compiler may or may not abide to. A good compiler will anyways do what needs to be done.

While, the truth:

inline usually indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.

So If your purpose of usage is optimizations the answer is:

YES, You can stop using inline. Most of modern day compilers will do that for you quite nicely.

But if your purpose of usage of inline is to allow you to get past the one definition rule and define a function body in a header file without breaking the ODR then answer is:

NO, You need to explicitly mark the functions as inline to be able to bypass the ODR.

Note: Member functions defined within the class body are implicitly inline, but same does not apply for free functions.

like image 22
Alok Save Avatar answered Oct 02 '22 18:10

Alok Save