当我们不能声明函数内联(GCC 编译器)时?
When we mustn't declare function inline(GCC compiler)?
我需要编写一个程序,以发现内联函数使用的限制。
我找到了GCC编译器的以下信息(http://gcc.gnu.org/onlinedocs/gcc/inline.html#inline):
请注意,函数定义中的某些用法可能使其不适合内联替换。这些用法包括:variadic功能,使用同种使用,使用计算的goto(请参见标签作为值),使用非局部goto,使用嵌套功能,使用setJMP,使用__builtin_longjmp以及__builtin_longjmp的使用以及__builtintin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_retin_ertin_retinter或__builtintin_builtintin_apply_apply_apply_args。使用-Winline警告,当无法替换为内联函数并给出故障的原因。
。然后我用variadic函数编写了以下程序:
#include <cstdarg>
#include <iostream>
using namespace std;
inline double average(int count, ...) {
va_list ap;
int j;
double sum = 0;
va_start(ap, count);
for (j = 0; j < count; j++) {
/* Increments ap to the next argument. */
sum += va_arg(ap, int);
}
va_end(ap);
return sum / count;
}
int main(void) {
cout << average(4,6,8,2,3);
return 0;
}
然后像这样编译我的程序:g++ -Wall -Winline program.cpp
。汇编后,-Winline
没有警告。
我做错了什么?感谢您的回答!
您链接的页面:
GCC不优化
时不会内联任何功能
在命令行中添加-O2在Godbolt中产生以下警告:
<source>: In function 'double average(int, ...)':
<source>:6:15: warning: function 'double average(int, ...)' can never be inlined because it uses variable argument lists [-Winline]
inline double average(int count, ...)
^~~~~~~
<source>: In function 'int main()':
<source>:6:15: warning: inlining failed in call to 'double average(int, ...)': function not inlinable [-Winline]
<source>:23:30: note: called from here
cout << average(4,6,8,2,3);
^
相关文章:
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 初始化迭代器错误 C++ 在 GCC 编译器中
- 编译器资源管理器和 GCC 具有不同的输出
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 交叉编译:特殊的交叉编译器还是只是带有选项 -march 的 gcc?
- #pragma(*诊断)当将Clang分析器与GCC编译器混合时
- 为什么 std::count 比 MSVC 编译器的普通循环慢,但与 GCC 相等?
- 为什么 gcc 编译器标志未知?
- 不同版本的编译器(例如GCC)是否会产生不同的性能?
- 如何使用 GCC 编译器优化创建静态库?
- C++使用旧编译器GCC 4.4.0在结构列表中查找字符串
- Meson选择了错误的编译器(GCC而不是在Clang上)
- 他们实现的开源C编译器(gcc,clang)的实际标准在哪里
- 编译器(GCC)如何处理C 的访问控件
- Netbeans 8.1(适用于 C/C++)找不到我的编译器(gcc-6.0.0 开发版本)
- 如何为在 .props 文件中声明的非本机 VC 编译器 (GCC) 实现 MSBUILD 文件跟踪功能 (跟踪器.ex
- 编译器(gcc)能否保证c ++循环
- 我如何在 Xcode 中使用指定的编译器 GCC-MP-4.7
- 编译器(GCC)交叉编译(对windows)如何在非源目录下构建它
- 如何解释编译系统应该使用哪种编译器(gcc)