内联方法:内部类与外部类定义 [英] Inline method: inside vs outside class definition

查看:113
本文介绍了内联方法:内部类与外部类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有一个方法,并且想给编译器提示内联它是一个好主意,那么您目前有两种解决方案.第一个是在声明类时定义方法:

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];
    }
    ...
}

由于此方法可能会降低可读性,因此另一种解决方案是使用inline关键字并在类外定义该方法:

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];
}

这使代码更具可读性(至少我更喜欢).阅读我的 STL 实现后,我发现它们混合使用了两者.在类中定义了一些方法(我认为应该真正内联的那些方法),而其他方法是使用inline关键字在类外定义的.该文件还以该类的带注释的声明开始.

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.

所以我的问题如下.当前的编译器(我正在考虑 GCC Visual Studio )比使用inline关键字在类外声明的成员函数更有可能内联在类内部声明的成员函数?

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?

备注:此问题不是,因为我的问题是关于编译器实现的.用这两种方式表示希望内联这些功能是等效的. STL的编写方式表明它们不是.

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.

推荐答案

inline关键字被认为是对编译器的提示,但是大多数编译器在确定内联内容方面比程序员要好得多,因此他们通常会忽略此提示.

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.

如今,inline关键字的主要用法(仅用于?)允许在标头中定义函数,而不会产生多个定义链接错误(与内联无关).

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)).

这篇关于内联方法:内部类与外部类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆