C ++模板和内联 [英] C++ template and inline

查看:126
本文介绍了C ++模板和内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写一个简单的(非模板)类时,如果函数实现是就地提供的,它会被自动处理为 inline p>

When I'm writing a simple (non-template) class, if the function implementation is provided "right in place", it's automatically treated as inline.

class A {
   void InlinedFunction() { int a = 0; }
   // ^^^^ the same as 'inline void InlinedFunction'
}

当谈论基于模板的类时,这个规则怎么样?

What about this rule when talking about template-based classes?

template <typename T> class B {
   void DontKnowFunction() { T a = 0; }
   // Will this function be treated as inline when the compiler
   // instantiates the template?
};

此外,如何应用 inline 到非嵌套模板函数,如

Also, how is the inline rule applied to non-nested template functions, like

template <typename T> void B::DontKnowFunction() { T a = 0; }

template <typename T> inline void B::DontKnowFunction() { T a = 0; }

在第一种情况和第二种情况下会发生什么?

What would happen in the first and in the second case here?

谢谢。

推荐答案

我知道的模板函数自动内联。然而,现实是大多数现代编译器经常忽略内联限定符。编译器的优化启发式方法很可能比人类程序员更好地选择内联函数。

Templated functions as far as I know are automatically inline. However, the reality is that most modern compilers regularly ignore the inline qualifier. The compiler's optimizing heuristics will most likely do a far better job of choosing which functions to inline than a human programmer.

这篇关于C ++模板和内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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