在模板中使用内嵌关键字是否有意义?(Does it make any sense to use inline keyword with templates?)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
在模板中使用内嵌关键字是否有意义?(Does it make any sense to use inline keyword with templates?)

由于模板在头文件中被定义,编译器能够确定内联函数是否有利,是否有意义? 我听说现代编译器知道什么时候嵌入函数,并忽略inline提示。


编辑:我想接受这两个答案,但这是不可能的。 为了解决这个问题,我收到了广泛的答复,因为它收到了大部分的投票,他是正式的,但正如我在评论中提到的那样,我认为小狗组件10的答案是正确的,从不同的角度来看。

问题在于C ++语义,这在内inline关键字和内联的情况下是不严格的。 phresnel说“如果你的意思是内联”,但是inline意味着什么是不清楚的,因为它从原来的意义演变成一个指令,“ Puppy说”停止编译器嘲弄ODR违规。

Since templates are defined within headers and compiler is able to determine if inlining a function is advantageous, does it make any sense? I've heard that modern compilers know better when to inline a function and are ignoring inline hint.


edit: I would like to accept both answers, but this is not possible. To close the issue I am accepting phresnel's answer, because it received most votes and he is formally right, but as I mentioned in comments I consider Puppy's and Component 10's answers as correct ones too, from different point of view.

The problem is in C++ semantics, which is not strict in case of inline keyword and inlining. phresnel says "write inline if you mean it", but what is actually meant by inline is not clear as it evolved from its original meaning to a directive that "stops compilers bitching about ODR violations" as Puppy says.

最满意答案

这并不无关紧要。 而不是,默认情况下不是每个函数模板都是inline的。 该标准在显式专业化 ([temp.expl.spec])中甚至是明确的)

有以下几点:

a.cc

#include "tpl.h"

b.cc

#include "tpl.h"

tpl.h (取自显性专业):

#ifndef TPL_H #define TPL_H template<class T> void f(T) {} template<class T> inline T g(T) {} template<> inline void f<>(int) {} // OK: inline template<> int g<>(int) {} // error: not inline #endif

编译这个,

g++ a.cc b.cc /tmp/ccfWLeDX.o: In function `int g<int>(int)': inlinexx2.cc:(.text+0x0): multiple definition of `int g<int>(int)' /tmp/ccUa4K20.o:inlinexx.cc:(.text+0x0): first defined here collect2: ld returned 1 exit status

在进行明确的实例化时,不表示inline也可能导致问题。

所以总结一下 :对于非完全专门的功能模板,即至少包含一个未知类型的功能模板,可以省略inline ,也不会收到错误,但仍然不是inline 。 对于完全专业化,即只使用已知类型的专业化,您不能省略。

建议的经验法则 :如果您的意思是写入内容,并且只是一致。 它让你思考是否或仅仅因为可以。 (这个经验法则符合Vandevoorde / Josuttis的C ++模板:完整指南 )。

It is not irrelevant. And no, not every function template is inline by default. The standard is even explicit about it in Explicit specialization ([temp.expl.spec])

Have the following:

a.cc

#include "tpl.h"

b.cc

#include "tpl.h"

tpl.h (taken from Explicit Specialization):

#ifndef TPL_H #define TPL_H template<class T> void f(T) {} template<class T> inline T g(T) {} template<> inline void f<>(int) {} // OK: inline template<> int g<>(int) {} // error: not inline #endif

Compile this, et voila:

g++ a.cc b.cc /tmp/ccfWLeDX.o: In function `int g<int>(int)': inlinexx2.cc:(.text+0x0): multiple definition of `int g<int>(int)' /tmp/ccUa4K20.o:inlinexx.cc:(.text+0x0): first defined here collect2: ld returned 1 exit status

Not stating inline when doing explicit instantiation may also lead to issues.

So in summary: For non fully specialized function templates, i.e. ones that carry at least one unknown type, you can omit inline, and not receive errors, but still they are not inline. For full specializations, i.e. ones that use only known types, you cannot omit it.

Proposed rule of thumb: Write inline if you mean it and just be consistent. It makes you think less about whether to or not to just because you can. (This rule of thumb is conforming to Vandevoorde's/Josuttis's C++ Template: The Complete Guide).

更多推荐

本文发布于:2023-04-13 12:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6fb095e928f593234a119e46a8194ac0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有意义   内嵌   关键字   模板   keyword

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!