GCC __attribute __((always_inline))和lambdas,这个语法是否正确? [英] GCC __attribute__((always_inline)) and lambdas, is this syntax correct?

查看:598
本文介绍了GCC __attribute __((always_inline))和lambdas,这个语法是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC 4.6作为Cortex嵌入式处理器的lpcxpresso ide的一部分。我有非常有限的代码大小,特别是在调试模式下编译时。使用属性((always_inline))已经证明是一个很好的工具,用于内联琐碎的函数,这样可以在调试模式下节省大量的代码,同时仍保持可读性。我期望它在某种程度上是主流,并在未来得到支持,因为这里提到 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348c/CIAJGAIH.html



现在到我的问题:这是正确的语法声明Lambda总是内联?

  #define ALWAYS_INLINE __attribute __((always_inline))
[](volatile int& i)ALWAYS_INLINE {i ++; }

它工作,我的问题是它将来会继续工作,确保它在未来工作。如果我切换到另一个支持c ++ 11的主要编译器,我会找到一个类似的关键字,我可以替换属性((always_inline))?
如果我要见到我的仙女教母,我希望一个编译器指令,它导致所有被构造为临时的空构造函数和通过引用绑定的lambdas即使在调试模式下也自动内联。

解决方案


以后会继续使用吗?


很可能,但是 always_inline 是编译器特定的,并且由于没有标准指定其与lambda的确切行为,


我能做些什么来确保它能正常工作?


这取决于编译器不是你。如果将来的版本不再支持 always_inline 和lambda,你必须坚持一个版本的工作或代码你自己的预处理器内联lambdas与 always_inline

如果我切换到另一个支持c ++ 11的主要编译器,我将$
找到一个类似的关键字?


很可惜,但没有保证。唯一真正的标准是C ++ inline 关键字,它不适用于lambdas。对于非lambda,它只建议内联,并告诉编译器可以在不同的编译单元中定义一个函数。


I am using GCC 4.6 as part of the lpcxpresso ide for a Cortex embedded processor. I have very limited code size, especially when compiling in debug mode. Using attribute((always_inline)) has so far proven to be a good tool to inline trivial functions and this saves a lot of code bloat in debug mode while still maintaining readability. I expect it to be somewhat mainstream and supported in the future because it is mentioned here http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348c/CIAJGAIH.html

Now to my question: Is this the correct Syntax for declaring a Lambda always inline?

#define ALWAYS_INLINE __attribute__((always_inline))
[](volatile int &i)ALWAYS_INLINE{i++;}

It does work, my question is will it continue to work in future and what can I do to ensure it works in the future. If I ever switch to another major compiler that supports c++11 will I find a similar keyword which I can replace the attribute((always_inline)) with? If I were to meet my fairy godmother I would wish for a compiler directive which causes all lambdas which are constructed as temporaries with empty constructors and bound by reference to be automatically inlined even in debug mode. Any ideas?

解决方案

Will it continue to work in future?

Likely but, always_inline is compiler specific and since there is no standard specifying its exact behavior with lambda, there is no guaranty that this will continue to work in the future.

What can I do to ensure it works?

This depends on the compiler not you. If a future version drops support for always_inline with lambda, you have to stick with a version that works or code your own preprocessor that inlines lambdas with an always_inline-like keyword.

If I ever switch to another major compiler that supports c++11 will I find a similar keyword?

Likely but again, there is no guaranty. The only real standard is the C++ inline keyword and it is not applicable to lambdas. For non-lambda it only suggests inlining and tells the compiler that a function may be defined in different compile units.

这篇关于GCC __attribute __((always_inline))和lambdas,这个语法是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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