是C ++ 11编译器内联的std ::函数吗? [英] Are std::functions inlined by the C++11 compiler?

查看:173
本文介绍了是C ++ 11编译器内联的std ::函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++ 11中的一个小数学优化框架,我不知道什么是用户提供领域特定逻辑的最好方法。我可以强迫她使用钩子方法来定义类,可以被框架调用,但我想保持精益,并尽可能利用新的C ++ 11设施。所以我想接受 std :: function 对象,可能从lambda表达式实例化,作为参数,并在需要时调用它们。我唯一想到的是,编译器(在我的情况下,gcc,但我想知道的Xcode和Visual C ++)将能够采取std ::函数对象和内联函数定义,所以它们与其余的代码一起优化。



PS:从评论,看起来像我的问题的第一个修订是模糊的大多数用户,可能是我使用错误的语言的错误。所以我重写了它,我希望有人能够理解我试图在这里传达的概念(并可能建议一个解决方案)。



PPS:有人建议使用模板,这是我想过的一个想法,但我想知道是否有另一个选择。我没有什么反对模板,但我打算做一个基于模板的版本,一旦这一个工作,因为我发现更容易推理的动态对象。

解决方案

std :: function 是可以存储任何可调用对象的可调用对象。它通过设计尽可能灵活。然而这种设计有缺点。因为 function 可以包含几乎任何东西(可调用),他们必须能够使用任何



在内部, function 在编译时不知道它可能存储什么。因为这个确定是一个运行时,它几乎不可能的大多数正常的编译器内联通过。内联函数指针是可能的,但只有当它在本地知道该指针的值是什么。 函数比一个简单的函数指针复杂得多。



理论上可以内联这样的东西,而是作为某种简档引导优化系统的一部分,其中代码的重复执行能够确定某些函数对象将总是与某些内容一起使用,并且因此以内联它们。但是,即使这是



如果你想内联一个任意可调用对象,你应该使用一个模板函数,而不是 std :: function 函数用于不能使用模板(可能是因为您需要在容器中存储多个函数,或者因为您不想破解封装,或者什么),但仍然需要存储任意可调用。


I'm working on a small mathematical optimization framework in C++11, and I wonder what's the best way for the user to provide domain-specific logic. I could force her to define classes with hook methods that can be called by the framework, but I'd like to keep it lean and take advantage of the new C++11 facilities whenever I can. So I'm thinking about accepting std::function objects, possibly instantiated from lambda expressions, as parameters, and call them when needed. The only think I'm wondering about is whether the compiler (in my case gcc, but I'd like to know about Xcode and Visual C++ as well) will be able to take the std::function objects and inline the function definitions, so that they are optimized together with the rest of the code.

PS: from the comments, it looks like the first revision of my question was obscure to most of the users, probably my fault for using an incorrect language. So I have reworded it, I hope someone can understand the concept I'm trying to convey here (and possibly suggest a solution).

PPS: someone suggested to use templates, that is an idea I have thought about, but I'd like to know if there's an alternative. I don't have anything against templates, but I plan to make a template-based version as soon as this one is working because I find it easier to reason in terms of dynamic objects.

解决方案

std::function is a callable object which can store any callable object. It is as flexible as possible, by design. That design however comes with drawbacks. Because functions could contain pretty much anything (callable), they must be able to work with anything.

Internally, function has no idea at compile-time what it might be storing. Since this determination is made a runtime, it is pretty much impossible for most normal compilers to inline through that. Inlining through a function pointer is possible, but only if it's locally known what the value of that pointer is. function is a lot more complex than a mere function pointer.

It is theoretically possible to inline such a thing, but only as part of some kind of profile-guided optimization system, where repeated execution of the code is able to determine that certain function objects will always be used with certain contents and thus to inline them. But even that is

If you want inlining for an arbitrary callable object, you should use a template function, not std::function. function is for when you can't use templates (perhaps because you need to store multiple functions in a container, or because you don't want to break encapsulation, or whatever), yet still need to store arbitrary callables.

这篇关于是C ++ 11编译器内联的std ::函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆