C++ 当我们必须';是否声明函数内联(GCC编译器)?
我需要编写一个程序,找出内联函数的使用限制 我找到了GCC编译器()的以下信息: 请注意,函数定义中的某些用法可能使其不适合内联替换。这些用法包括:变量函数、alloca的使用、计算goto的使用(参见标签作为值)、非局部goto的使用、嵌套函数的使用、setjmp的使用、内置longjmp的使用以及内置return或内置apply参数的使用。当无法替换标记为inline的函数时,使用-Winline发出警告,并给出失败的原因 然后我用可变函数编写了以下程序:C++ 当我们必须';是否声明函数内联(GCC编译器)?,c++,gcc,C++,Gcc,我需要编写一个程序,找出内联函数的使用限制 我找到了GCC编译器()的以下信息: 请注意,函数定义中的某些用法可能使其不适合内联替换。这些用法包括:变量函数、alloca的使用、计算goto的使用(参见标签作为值)、非局部goto的使用、嵌套函数的使用、setjmp的使用、内置longjmp的使用以及内置return或内置apply参数的使用。当无法替换标记为inline的函数时,使用-Winline发出警告,并给出失败的原因 然后我用可变函数编写了以下程序: #include <cstd
#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;
}
#包括
#包括
使用名称空间std;
内联双平均(整数计数,…){
va_列表ap;
int j;
双和=0;
va_启动(ap,计数);
对于(j=0;j 从您链接的页面上看:
GCC在未优化时不内联任何函数
将-O2添加到命令行会在中产生以下警告:
:在函数“双平均值(int,…)”中:
:6:15:警告:函数“双平均值(int,…”永远不能内联,因为它使用变量参数列表[-Winline]
内联双平均(整数计数,…)
^~~~~~~
:在函数“int main()”中:
:6:15:警告:调用“双平均(int,…”:函数不可内联[-Winline]时内联失败
:23:30:注意:从这里呼叫
库特
<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);
^