编译器会忽略我的函数的内联限定符吗?
Will compiler ignore inline qualifier for my function?
我读到在一个函数中有多于一行将伪造 "inline",如果是这样,我如何知道我的函数何时被内联,反之亦然:/
inline int foo(int x, int y)
{
cout<<"foo-boo";
return (x > y)? x : y;
}
inline
与函数中的行数1 没有任何关系。它只是一个编译器提示,编译器无论如何不是,必须遵循。函数在声明时是否真正内联 inline
,是实现定义的。
来自 C++14 标准草案 N3690,§7.1.2:
A function declaration (8.3.5, 9.3, 11.3) with an inline
specifier
declares an inline function. The inline
specifier indicates to the
implementation that inline substitution of the function body at the
point of call is to be preferred to the usual function call mechanism.
An implementation is not required to perform this inline substitution at the point of call [...]
(格式化我的。)
有特定于编译器的选项和属性 enable/disable 内联所有函数和做其他相关的事情。查看编译器的文档以获取更多信息。
1 编译器在决定是否内联函数时可以考虑函数的行数,但这是实现定义的,标准不需要。
记住,内联只是对编译器的请求,而不是命令。编译器可以忽略内联请求。在以下情况下,编译器可能不会执行内联:
- 如果函数包含循环。 (为了,一会儿,一会儿)
- 如果函数包含静态变量。
- 如果函数是递归的。
- 如果函数return类型不是void,函数体中不存在return语句
- 如果函数包含 switch 或 goto 语句。
我读到在一个函数中有多于一行将伪造 "inline",如果是这样,我如何知道我的函数何时被内联,反之亦然:/
inline int foo(int x, int y)
{
cout<<"foo-boo";
return (x > y)? x : y;
}
inline
与函数中的行数1 没有任何关系。它只是一个编译器提示,编译器无论如何不是,必须遵循。函数在声明时是否真正内联 inline
,是实现定义的。
来自 C++14 标准草案 N3690,§7.1.2:
A function declaration (8.3.5, 9.3, 11.3) with an
inline
specifier declares an inline function. Theinline
specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call [...]
(格式化我的。)
有特定于编译器的选项和属性 enable/disable 内联所有函数和做其他相关的事情。查看编译器的文档以获取更多信息。
1 编译器在决定是否内联函数时可以考虑函数的行数,但这是实现定义的,标准不需要。
记住,内联只是对编译器的请求,而不是命令。编译器可以忽略内联请求。在以下情况下,编译器可能不会执行内联:
- 如果函数包含循环。 (为了,一会儿,一会儿)
- 如果函数包含静态变量。
- 如果函数是递归的。
- 如果函数return类型不是void,函数体中不存在return语句
- 如果函数包含 switch 或 goto 语句。