C++ template and inline(C++ 模板和内联)
问题描述
当我编写一个简单的(非模板)类时,如果函数实现是就地"提供的,它会自动被视为inline
.
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++ 模板和内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 模板和内联


基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01