如果我为一个类实现一个运算符,它是否有可能不会是内联的?
运算符总是内联吗?
struct foo {
void operator ()() {
// Do tons of work.
}
};
int main() {
foo f;
f();
}
Are operators always inlined?
struct foo {
void operator ()() {
// Do tons of work.
}
};
int main() {
foo f;
f();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与任何其他函数一样,运算符是一个普通函数。
An operator is a normal function just as any other function.
编译器是史无前例且(官方)不可预测的内联决策之主。好的编译器会在文档中提供一些有关其实现行为的指导。代码越复杂,内联的可能性就越小,您可以在维基百科上找到一些倾向于内联/不倾向于内联的示例。
“做大量工作”本身表明您想要的运算符对于大多数编译器来说太复杂而无法内联。
当 Microsoft 的 Visual C++ 编译器决定内联未标记为内联的函数以及不内联标记为内联的函数时,可以使它生成警告。我喜欢它来感受它可以内联什么。
The compiler is the unprecedented and (officially) unpredictable lord of inlining decisions. Good compilers will provide some guidance in the documentation about their implementations behaviour. The more complicated the code the less likely it is to be inlined, you can find some examples of what does/doesn't tend to inline on the Wikipedia.
"Do tons of work" on it's own suggests that your intended operator is too complicated for most compilers to inline.
Microsoft's Visual C++ compiler can be made to generate warnings, when it decides to inline a function that wasn't marked inline and when it doesn't inline one that was marked inline. I like it for getting a feel for what it can inline.
不,他们不是。编译器可以完全自由地忽略所有内联函数的请求。它不能忽略的是它必须给它们内部链接,因此包含它们的标头可以包含在多个翻译单元中。
No, they are not. The compiler is completely free to ignore all and any requests that a function be inlined. What it can't ignore is that it must give them internal linkage, so a header containing them can be included in multiple translation units.