使用内联关键字与模板有什么意义吗? [英] Does it make any sense to use inline keyword with templates?

查看:166
本文介绍了使用内联关键字与模板有什么意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于模板在头文件中定义,编译器能够确定内联函数是否有利,它是否有意义?我听说现代编译器知道更好的内联函数并且忽略 inline 提示。






编辑:我想接受这两个答案,但这是不可能的。要关闭此问题,我接受 phresnel 的答案,因为它获得了大多数投票,而且他是正式的,但正如我在评论中提到的,我认为 Puppy



问题是在C ++语义中,这是不严格的情况下 inline 关键字和内联。 phresnel 表示如果您的意思是写入内容,但 inline 实际意味着不清楚,因为它从原来的意义演变为指示

解决方案

阻止编译器叮咬ODR违规。 。没有,不是每个函数模板 inline 默认情况下。 ([temp.expl.spec])



具有以下功能:



a.cc

  #includetpl.h 

b.cc

  #includetpl.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

Compile this,et voila:

  g ++ a.cc b.cc 
/tmp/ccfWLeDX.o:In函数`int g< int>(int)':
inlinexx2.cc:(.text+0x0):`int g< int>(int)'的多重定义
/tmp/ccUa4K20.o :inlinexx.cc :(。text + 0x0):首先在这里定义
collect2:ld返回1退出状态

当进行显式实例化时,不会说明 inline 也可能导致问题。



:对于非完全专用的函数模板,即包含至少一个未知类型的模板,您可以省略 inline ,但不接收错误,不是 inline



建议的经验法则:写入 inline 如果你的意思,只是一致。它让你思考,不管是否只是因为你可以。 (此经验法则符合 Vandevoorde / Josuttis的C ++范本:完整指南 ) $ b

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.

解决方案

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).

这篇关于使用内联关键字与模板有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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