内联和良好做法 [英] inline and good practices
问题描述
我已经看到许多使用不同语法的源代码关于 inline
指令。
I have seen many source codes using different syntaxes regarding the inline
directive.
namespace Foo
{
class Bar
{
public:
// 1 - inline on the declaration + implementation
inline int sum1(int a, int b) { return a + b; }
// 2 - inline on template declaration + implementation
template <typename T>
inline T sum2(T a, T b) { return a + b; }
// 3 - Nothing special on the declaration...
int sum3(int a, int b);
};
// 3 - But the inline directive goes after
// In the same namespace and still in the header file
inline int Bar::sum3(int a, int b) { return a + b; }
}
我没有找到关于 inline
:我只知道 inline
只是对编译器的一个提示, 。
I failed to find an "official" guidelines regarding the usage of inline
: I only know that I know not much about it.inline
is just a hint to the compiler and that it enforces internal linkage.
这是我的问题:
- 是(1)好习惯吗?
- 在(2)中,
inline
? (我的猜测是不,但我不能解释为什么)。需要什么时候? - (3)似乎是最常用的语法。这有什么不对的地方或者我应该使用它吗?
- 有
在线
的任何其他用途(语法),我不知道的?
- Is (1) good practice ?
- In (2), is the
inline
directive always needed ? (My guess would be "no", but I can't explain why). When is it needed ? - (3) seems to be the most used syntax. Is there anything wrong with it or should I use it too ?
- Is there any other use (syntax) of
inline
I am unaware of ?
推荐答案
不, inline
不是 提示编译器,它不强制内部链接。
No, and no! inline
is not just a hint to the compiler and it doesn't enforce internal linkage.
inline
隐含在类主体中定义的函数上,因此您只需要在类外部定义的函数上使用它。什么时候应该使用它,只有当你需要启用更改的一个定义规则的是在线
做。
inline
is implicit on functions defined in a class body so you only need it on functions defined outside of classes. You should use it when, and only when, you need to enable the changes to the one definition rule that inline
makes.
这篇关于内联和良好做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!