C++ 使 getter 和 setter 内联是一种好习惯吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3017264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:48:05  来源:igfitidea点击:

Is it good practice to make getters and setters inline?

c++performanceinlinegetter-setter

提问by Martijn Courteaux

public:
     inline int GetValue() const {
          return m_nValue;
     }
     inline void SetValue(int nNewValue) {
          this -> m_nValue = nNewValue;
     }

On Learn C++, they said it would run faster. So, I thought it would be great to use on getters and setters. But maybe, there are some drawbacks to it?

Learn C++ 上,他们说它会运行得更快。所以,我认为在 getter 和 setter 上使用会很棒。但也许,它有一些缺点?

采纳答案by JaredPar

I don't inline anything until a profiler has specifically told me that not inlining is resulting in a performance problem.

我不会内联任何东西,直到分析器明确告诉我不内联会导致性能问题。

The C++ compiler is very smart and will almost certainly automatically inline such simple function like this for you. And typically it's smarter than you are and will do a much better job at determining what should or should not be inlined.

C++ 编译器非常聪明,几乎肯定会自动为您内联这样简单的函数。通常它比你更聪明,并且会在确定什么应该或不应该内联方面做得更好。

I would avoid thinking about what to or not to inline and focus on the solution. Adding the inlinekeyword later (which is not a guarantee of inline BTW) is very easy to do and potential places can be found readily with a profiler.

我会避免考虑要内联或不内联的内容,而是专注于解决方案。inline稍后添加关键字(这不是内联 BTW 的保证)很容易做到,并且可以使用分析器轻松找到潜在的位置。

回答by AraK

If you write them inside the definition, they are considered inlineby default.

如果您将它们写在定义中,则inline默认情况下会考虑它们。

This means that they will be permitted in multiple compilation units (since class definitions themselves typically appear in multiple compilation units), notthat they will actually beinlined.

这意味着它们将被允许在多个编译单元中(因为类定义本身通常出现在多个编译单元中),而不是它们实际上会被内联。

回答by Greg Domjan

This is Bad practice in public API's Any change to these functions requires recompilation of all clients.

这是公共 API 中的错误做法。对这些函数的任何更改都需要重新编译所有客户端。

In generalhaving getters and setters is showing poor abstraction, don't do it. If you are constantly going to the raw data in another class then you likely need to re arrange your classes, instead consider how you wish to manipulate the data within a class and provide appropriate methods for doing so.

一般来说,拥有 getter 和 setter 表现出糟糕的抽象,不要这样做。如果您经常访问另一个类中的原始数据,那么您可能需要重新安排您的类,而不是考虑您希望如何操作类中的数据并为此提供适当的方法。

回答by Edward Strange

Negative points:

负面观点:

  1. The compiler is free to ignore you.

  2. Any change to these functions requires recompilation of all clients.

  3. A good compiler will inline non-inlined functions anyway when it is appropriate.

  1. 编译器可以随意忽略您。

  2. 对这些函数的任何更改都需要重新编译所有客户端。

  3. 一个好的编译器会在适当的时候内联非内联函数。

回答by Shane

I'd also like to add that unless you're performing millions of gets/sets per frame, it's pretty much irrelevant whether these are inlined or not. It's honestly not worth losing sleep over.

我还想补充一点,除非您每帧执行数百万次获取/设置,否则这些是否内联几乎无关紧要。老实说,不值得为此失眠。

Also, keep in mind that just because you put the word 'inline' in front of your declaration+definition, doesn't mean the compiler will inline your code. It's uses various heuristics to work out whether it makes sense, which is often the classic trade off of speed vs size. There is however the brute force '__forceinline' keyword, at lease in VC++ (I'm not sure what it is in GCC), which stomps on the compilers fancy heuristics. I really don't recommend it at all, and besides once you port to a different architecture, it'll likely be incorrect.

另外,请记住,仅仅因为您将“内联”一词放在声明+定义的前面,并不意味着编译器会内联您的代码。它使用各种启发式方法来确定它是否有意义,这通常是速度与大小的经典权衡。然而,在 VC++ 中至少有一个蛮力 '__forceinline' 关键字(我不确定它在 GCC 中是什么),它踩踏了编译器花哨的启发式方法。我真的不推荐它,而且一旦你移植到不同的架构,它可能是不正确的。

Try to put all function definitions in the implementation file, and leave the pure declarations for the headers (unless of course you're template metaprogramming (STL/BOOST/etc), in which case, pretty much everything is in the headers ;))

尝试将所有函数定义放在实现文件中,并保留头文件的纯声明(当然,除非您是模板元编程(STL/BOOST/等),在这种情况下,几乎所有内容都在头文件中;))

One of the classic places people like to inline (at least in video games, which is where I'm from), is in math headers. Cross/dot products, vector lengths, matrix clearing, etc are often placed in the header, which I just think is unnecessary. 9/10 it makes no difference to performance, and if you ever need to do a tight loop, such as transforming a large vector array by some matrix, you're probably better off manually doing the math inline, or even better coding it in platform-specific assembler.

人们喜欢内联的经典位置之一(至少在我来自的视频游戏中)是在数学标题中。交叉/点积、向量长度、矩阵清除等通常放在标题中,我认为这是不必要的。9/10 它对性能没有影响,如果你需要做一个紧密的循环,比如用某个矩阵转换一个大的向量数组,你可能最好手动进行内联数学运算,或者更好地编码它特定于平台的汇编程序。

Oh, and another point, if you feel you really need a class to be more data than code, consider using good old struct, which doesn't bring the OO baggage of abstraction with it, that's what it's there for. :)

哦,还有一点,如果你觉得你真的需要一个类来比代码更多的数据,考虑使用好的旧结构,它不会带来抽象的面向对象包袱,这就是它的目的。:)

Sorry, didn't mean to go on so much, but I just think it helps to consider real world use cases, and not get too hung up on pedantic compiler settings (trust me, I've been there ;))

抱歉,我不是故意要讲这么多,但我只是认为考虑现实世界的用例会有所帮助,而不是太拘泥于迂腐的编译器设置(相信我,我去过那里;))

Good luck.

祝你好运。

Shane

沙恩

回答by Gal

The inline keyword is meaningless in your case

inline 关键字在您的情况下毫无意义

Compiler will inline your function if it can and wants to, regardless of keyword.

编译器将内联你的函数,如果它可以并且想要的话,不管关键字如何。

The keyword inline affects linking and not inlining. It is a bit confusing, but read up on it.

关键字 inline 影响链接而不是内联。这有点令人困惑,但请仔细阅读。

If the definition is in a different compilation unit (source file after preprocessor, basically) than the call, inlining will only be possible if whole project optimization and link time code generation are enabled. Enabling it greatly increases linking time (since it practically re-compiles everything in the linker), but obviously may improve performance. Not sure if it's on or off by default in GCC and VS.

如果定义在与调用不同的编译单元(预处理器之后的源文件,基本上)中,则只有在启用整个项目优化和链接时间代码生成的情况下才能进行内联。启用它会大大增加链接时间(因为它实际上会重新编译链接器中的所有内容),但显然可以提高性能。不确定它在 GCC 和 VS 中默认是打开还是关闭。

回答by Paul

The code will compile slightly longer and you lose encapsulation. Everything depends on the size of the project and its nature. In most cases it's OK to make them inline if they do not have any complex logic.

代码编译时间会稍微长一些,并且会丢失封装。一切都取决于项目的规模及其性质。在大多数情况下,如果它们没有任何复杂的逻辑,则可以使它们内联。

BTW, you may skip inlineif you implement directly in class definition.

顺便说一句,inline如果您直接在类定义中实现,您可以跳过。

回答by Tim Rupe

By putting the code in the header, you are exposing your internal class workings. Clients may see this and make assumptions on how your class works. This can make it more difficult to change your class later without breaking client code.

通过将代码放在标题中,您将公开您的内部类工作。客户可能会看到这一点,并对您的课程如何运作做出假设。这会使以后在不破坏客户端代码的情况下更改类变得更加困难。

回答by Numan

No need, start trusting the compilers, at least for such optimizations!
"But not always"

不需要,开始信任编译器,至少对于这样的优化!
“但不总是”

回答by mkluwe

I'd say that you don't need to bother with that. Read the FAQ section about inlining.

我会说你不需要为此烦恼。阅读有关内联常见问题部分