由于模板在头文件中被定义,编译器能够确定内联函数是否有利,是否有意义? 我听说现代编译器知道什么时候嵌入函数,并忽略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 #endifCompile 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 statusNot 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).
更多推荐
发布评论