Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline method: inside vs outside class definition

Tags:

If you have a method and you want to give the compiler a hint that it is a good idea to inline it, you currently have two solutions. The first one is to define the methods when you declare your class:

class Vector {
private:
    double* data_;
    double* size_;
    double* capacity_;
public:
    double& operator[](int k) {
        return data_[k];
    }
    ...
}

As this method might reduce readability, another solution is to use the inline keyword and define the method out of class:

class Vector {
private:
    double* data_;
    double* size_;
    double* capacity_;
public:
    inline double& operator[](int k);
    ...
}

double& Vector::operator[](int k) {
    return data_[k];
}

This makes the code more readable (at least I prefer it). Reading my STL implementation, I found that they use a mix of the two. Some methods (those which I think should really be inlined) are defined in the class, and others are defined out of class with the inline keyword. The file also begins with a commented declaration of the class.

So my question is the following. Do current compilers (I am thinking of GCC, Clang, Intel, and Visual Studio) are more likely to inline a member function that is declared inside the class than a member function declared out of class with the inline keyword?

Remark: This question is not a duplicate of When should I write the keyword 'inline' for a function/method? as my question is about compiler implementations. Do these two ways of saying that you want those functions to be inlined are equivalent. The way the STL is written suggests that they are not.

like image 276
InsideLoop Avatar asked Mar 09 '15 10:03

InsideLoop


People also ask

How is inline function define outside class?

An equivalent way to declare an inline member function is to either declare it in the class with the inline keyword (and define the function outside of its class) or to define it outside of the class declaration using the inline keyword.

What do you mean by inline method?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Are all functions inside class inline?

In fact, all the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare inline function in the class then just declare the function inside the class and define it outside the class using inline keyword.

How do you define a method outside a class in C++?

Defining the method outside the class To do this, we specify the name of the class, followed by the operator :: (scope resolution), then followed by the name of the method. In the code example below, we will create a class myclass and define a method mymethod outside the class.


1 Answers

The inline keyword is considered a hint to compilers, however most compilers are much better at deciding what to inline than programmers so they usually ignore this hint.

The main (only?) use for the inline keyword nowadays it to allow functions to be defined in the header and not generate multiple definition link errors (which has nothing to do with inlining really).

Also please note that inlining happens at a function call site so it doesn't make sense to say a function is inlined since it may be inlined in some places and not in others (depending on the code around it).

My advice: use what you think is more readable since it will have no impact on actual inlining (unless you use something compiler specific like __forceinline (don't do that)).

like image 85
Motti Avatar answered Sep 21 '22 03:09

Motti