当我编写一个简单的(非模板)类时,如果函数实现是"正确"提供的,它会自动被视为inline.
class A {
void InlinedFunction() { int a = 0; }
// ^^^^ the same as 'inline void InlinedFunction'
}
Run Code Online (Sandbox Code Playgroud)
在谈论基于模板的类时,这条规则怎么样?
template <typename T> class B {
void DontKnowFunction() { T a = 0; }
// Will this function be treated as inline when the compiler
// instantiates the template?
};
Run Code Online (Sandbox Code Playgroud)
此外,该inline规则如何应用于非嵌套模板函数,例如
template <typename T> void B::DontKnowFunction() { T a = 0; }
template <typename T> inline void B::DontKnowFunction() { T a = 0; } …Run Code Online (Sandbox Code Playgroud)