1. <tfoot id='gM9Ke'></tfoot>
    2. <legend id='gM9Ke'><style id='gM9Ke'><dir id='gM9Ke'><q id='gM9Ke'></q></dir></style></legend>

        <i id='gM9Ke'><tr id='gM9Ke'><dt id='gM9Ke'><q id='gM9Ke'><span id='gM9Ke'><b id='gM9Ke'><form id='gM9Ke'><ins id='gM9Ke'></ins><ul id='gM9Ke'></ul><sub id='gM9Ke'></sub></form><legend id='gM9Ke'></legend><bdo id='gM9Ke'><pre id='gM9Ke'><center id='gM9Ke'></center></pre></bdo></b><th id='gM9Ke'></th></span></q></dt></tr></i><div id='gM9Ke'><tfoot id='gM9Ke'></tfoot><dl id='gM9Ke'><fieldset id='gM9Ke'></fieldset></dl></div>
          <bdo id='gM9Ke'></bdo><ul id='gM9Ke'></ul>

        <small id='gM9Ke'></small><noframes id='gM9Ke'>

      1. 应该永远不要使用静态内联函数吗?

        Should one never use static inline function?(应该永远不要使用静态内联函数吗?)
      2. <legend id='Us5k9'><style id='Us5k9'><dir id='Us5k9'><q id='Us5k9'></q></dir></style></legend>

      3. <small id='Us5k9'></small><noframes id='Us5k9'>

        <tfoot id='Us5k9'></tfoot>

          <i id='Us5k9'><tr id='Us5k9'><dt id='Us5k9'><q id='Us5k9'><span id='Us5k9'><b id='Us5k9'><form id='Us5k9'><ins id='Us5k9'></ins><ul id='Us5k9'></ul><sub id='Us5k9'></sub></form><legend id='Us5k9'></legend><bdo id='Us5k9'><pre id='Us5k9'><center id='Us5k9'></center></pre></bdo></b><th id='Us5k9'></th></span></q></dt></tr></i><div id='Us5k9'><tfoot id='Us5k9'></tfoot><dl id='Us5k9'><fieldset id='Us5k9'></fieldset></dl></div>

          • <bdo id='Us5k9'></bdo><ul id='Us5k9'></ul>

                    <tbody id='Us5k9'></tbody>
                  本文介绍了应该永远不要使用静态内联函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 inline 关键字有两个含义(第 7.1.3/4 节):

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

                  1. 暗示编译器在调用点替换函数体比通常的函数调用机制更可取.
                  2. 即使省略内联替换,也遵循内联的其他规则(尤其是 w.r.t 一个定义规则).
                  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.

                  如果需要,通常任何主流编译器都会在调用点替换函数体,因此不需要为#1标记函数inline.

                  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.

                  进一步 w.r.t #2,据我所知,当您将函数声明为 static inline 函数时,

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

                  函数上的static 关键字强制inline 函数有一个内部链接(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.

                  因此,实际上标记一个函数 staticinline 都没有任何用处.它应该是static(不是最喜欢的)或inline(最喜欢的),
                  那么,在函数上同时使用 staticinline 是否实际上没用?

                  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.

                  因此,实际上将函数标记为静态和内联根本没有用.它应该是静态的(不是最喜欢的)或内联的(最喜欢的),

                  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
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                    <tbody id='Vtce6'></tbody>

                    1. <legend id='Vtce6'><style id='Vtce6'><dir id='Vtce6'><q id='Vtce6'></q></dir></style></legend>

                        <small id='Vtce6'></small><noframes id='Vtce6'>

                      • <tfoot id='Vtce6'></tfoot>
                        <i id='Vtce6'><tr id='Vtce6'><dt id='Vtce6'><q id='Vtce6'><span id='Vtce6'><b id='Vtce6'><form id='Vtce6'><ins id='Vtce6'></ins><ul id='Vtce6'></ul><sub id='Vtce6'></sub></form><legend id='Vtce6'></legend><bdo id='Vtce6'><pre id='Vtce6'><center id='Vtce6'></center></pre></bdo></b><th id='Vtce6'></th></span></q></dt></tr></i><div id='Vtce6'><tfoot id='Vtce6'></tfoot><dl id='Vtce6'><fieldset id='Vtce6'></fieldset></dl></div>

                            <bdo id='Vtce6'></bdo><ul id='Vtce6'></ul>