在主函数之上定义命名空间函数是否会导致内联函数?
Does defining a namespace function above the main function result in an inline function?
如果我在 main 函数之上声明和定义命名空间函数 function,如下所示,是否会导致编译器将其生成为内联函数?
我知道当我们在 class 声明中定义一个函数时,编译器会将该函数生成为内联函数;但是,我不确定下面显示的特定案例是否属于这种情况。有好心人为我说明一下情况吗?
namespace pairs_with_k_diff {
void inc_and_push_back () {
// function definition
}
}
int main() {
// Call this function inside the main later
}
没有。函数定义在main函数之前还是之后,对函数是否内联没有影响。
相反,可以使用关键字 inline
将函数声明为内联。在 class 定义中定义的函数以及函数模板是隐式内联的。
您示例中的函数 pairs_with_k_diff::inc_and_push_back
不是内联函数,因为未使用 inline
关键字,并且它不是成员函数也不是函数模板。
as far as I know, is a function which a compiler can replace those function definition wherever those are being called.
不是这个意思。有关详细信息,请参阅 。
An inline function, as far as I know, is a function which a compiler can replace those function definition wherever those are being called.
所以,让我们把这部分弄清楚。
- It tells the compiler that the function code can be expanded where the function is called, instead of effectively being called.
- It tells the compiler that the function definition can be repeated.
您显然在考虑定义 1。随着答案的继续说明,该定义不再真正使用(如果曾经使用过的话)。 inline
对编译器只意味着定义可以重复。它允许您将定义放在 headers 中(尽管它也可能有其他用途)。
所以,回到你原来的问题:
If I were to declare and define a namespace function function above the main function, as it is shown below, would that result in compiler generating it as an inline function?
根据您的定义,内联只是 determined by the compiler。从这个意义上说,您无法控制 "inlineness,"。
当然,如果你想要inline的其他定义,你必须声明为inline:
namespace pairs_with_k_diff {
inline void inc_and_push_back () {
// function definition
}
}
没有其他方法可以得到这个函数"inline"(在这个词的第二个意义上),当然,不声明它inline
。它不会自动发生。
如果我在 main 函数之上声明和定义命名空间函数 function,如下所示,是否会导致编译器将其生成为内联函数?
我知道当我们在 class 声明中定义一个函数时,编译器会将该函数生成为内联函数;但是,我不确定下面显示的特定案例是否属于这种情况。有好心人为我说明一下情况吗?
namespace pairs_with_k_diff {
void inc_and_push_back () {
// function definition
}
}
int main() {
// Call this function inside the main later
}
没有。函数定义在main函数之前还是之后,对函数是否内联没有影响。
相反,可以使用关键字 inline
将函数声明为内联。在 class 定义中定义的函数以及函数模板是隐式内联的。
您示例中的函数 pairs_with_k_diff::inc_and_push_back
不是内联函数,因为未使用 inline
关键字,并且它不是成员函数也不是函数模板。
as far as I know, is a function which a compiler can replace those function definition wherever those are being called.
不是这个意思。有关详细信息,请参阅
An inline function, as far as I know, is a function which a compiler can replace those function definition wherever those are being called.
所以,让我们把这部分弄清楚。
- It tells the compiler that the function code can be expanded where the function is called, instead of effectively being called.
- It tells the compiler that the function definition can be repeated.
您显然在考虑定义 1。随着答案的继续说明,该定义不再真正使用(如果曾经使用过的话)。 inline
对编译器只意味着定义可以重复。它允许您将定义放在 headers 中(尽管它也可能有其他用途)。
所以,回到你原来的问题:
If I were to declare and define a namespace function function above the main function, as it is shown below, would that result in compiler generating it as an inline function?
根据您的定义,内联只是 determined by the compiler。从这个意义上说,您无法控制 "inlineness,"。
当然,如果你想要inline的其他定义,你必须声明为inline:
namespace pairs_with_k_diff {
inline void inc_and_push_back () {
// function definition
}
}
没有其他方法可以得到这个函数"inline"(在这个词的第二个意义上),当然,不声明它inline
。它不会自动发生。