应该从不使用静态内联函数吗? [英] Should one never use static inline function?

查看:237
本文介绍了应该从不使用静态内联函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 inline 关键字(§7.1.3 / 4)有两个影响:

There are two implications of using the inline keyword(§ 7.1.3/4):


  1. 提示编译器在调用点处替换函数体优于通常的函数调用机制。

  2. 则忽略行内替换,则遵循内联的其他规则(特别是wrt 一个定义规则)。

  1. It hints the compiler that substitution of function body at the point of call is preferable over the usual function call mechanism.
  2. Even if the inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.

通常任何主流编译器将在调用点处替换函数体,如果需要的话,则标记 inline 仅用于#1

Usually any mainstream compiler will substitute function body at the point of call if needed, so marking function inline merely for #1 is not really needed.

进一步写$ #2 ,我理解当你声明一个函数为 static inline 函数,

Further w.r.t #2, As I understand when you declare a function as static inline function,

函数上的 static inline 函数具有内部链接(内联函数具有外部链接)每个这样的函数的实例被视为单独的函数每个函数的地址是不同的),并且这些函数的每个实例具有它们自己的静态局部变量&字符串文字(内联函数只有这些的一个副本)

The static keyword on the function forces the inline function to have an internal linkage(inline functions have external linkage) Each instance of such a function is treated as a separate function(address of each function is different) and each instance of these functions have their own copies of static local variables & string literals(an inline function has only one copy of these)

因此,这样的函数像任何其他 static 函数和关键字 inline 不再重要,它变得多余。

Thus such a function acts like any other static function and the keyword inline has no importance anymore, it becomes redundant.

,实际上标记一个函数 static inline 都没有用。它应该是 static 不是最优选的)或 inline ),

所以,使用 static inline

So, Practically marking a function static and inline both has no use at all. Either it should be static(not most preferred) or inline(most preferred),
So, Is using static and inline together on a function practically useless?

推荐答案

您的分析是正确的,但不一定意味着无用。即使大多数编译器自动内联函数(原因#1),最好只是为了描述意图声明 inline

Your analysis is correct, but doesn't necessarily imply uselessness. Even if most compilers do automatically inline functions (reason #1), it's best to declare inline just to describe intent.

忽略与 inline static 函数的交互,应谨慎使用。在命名空间范围的 static 修饰符以前被废弃,支持未命名的命名空间(C ++ 03§D.2)。对于一些不清楚的原因,我不记得它已从C ++ 11中的弃用中删除,但你很少需要它。

Disregarding interaction with inline, static functions should be used sparingly. The static modifier at namespace scope was formerly deprecated in favor of unnamed namespaces (C++03 §D.2). For some obscure reason that I can't recall it was removed from deprecation in C++11 but you should seldom need it.


实际上标记一个函数static和inline都没有用。它应该是静态的(不是最优选的)或内联的(最优选的)。

So, Practically marking a function static and inline both has no use at all. Either it should be static(not most preferred) or inline(most preferred),

没有偏好的概念。 static 意味着具有相同签名的不同函数可能存在于不同的 .cpp 文件(翻译单位)中。 inline 没有 static 意味着对于不同的翻译单元,可以定义具有相同定义的相同函数。

There's no notion of preference. static implies that different functions with the same signature may exist in different .cpp files (translation units). inline without static means that it's OK for different translation units to define the same function with identical definitions.

首选是使用未命名的命名空间,而不是 static

What is preferred is to use an unnamed namespace instead of static:

namespace {
    inline void better(); // give the function a unique name
}

static inline void worse(); // kludge the linker to allowing duplicates

这篇关于应该从不使用静态内联函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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