A noinline inline function? What sorcery is this?

栏目: IT技术 · 发布时间: 3年前

内容简介:May 21st, 2020You can declare a noinline inline function.What sorcery is this, a function that is both inline and not-inline?
A noinline inline function? What sorcery is this?

Raymond

May 21st, 2020

You can declare a noinline inline function.

void g();

// gcc
__attribute__((noinline)) inline void f()
{
    g();
}

// MSVC
__declspec(noinline) inline void f()
{
    g();
}

void tryme()
{
    f();
    f();
}

What sorcery is this, a function that is both inline and not-inline?

The two keywords are not contradictory because they describe different senses of the word “inline”.

The C++ language keyword inline means “can be defined in multiple translation units without triggering an ODR violation.” In other words, it lets you put the function definition in a header file that is included by multiple C++ files.

The function attribute/declaration specifier noinline means “do not inline this function during code generation.” It is a directive to the optimizer not to perform inline substitution during code generation.

Historically, the inline C++ keyword was originally an optimizer hint, but optimizers were given permission to ignore it and make their own decisions about inline substitution during code generation. Nowadays, compilers pretty much ignore the optimization aspect of the inline keyword. The only thing that remains of the inline keyword is the ability to have multiple definitions without violating ODR.

You could say that the modern sense of the C++ keyword inline is “defined right here.” It’s a statement about the source code, not the object code.

In the above example, the function f is a noinline inline function. The inline keyword allows the definition of f to go into a header file that is consumed by multiple translation units. The noinline attribute/declaration specifier tells the optimizer to emit code for f and call it, rather than embedding the body of f into its call sites. The function tryme will call the function f twice, instead of optimizing out the call and just calling g twice.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

设计原本

设计原本

Frederick P. Brooks, Jr. / InfoQ中文站、王海鹏、高博 / 机械工业出版社 / 2011-1-1 / 55.00元

无论是软件开发、工程还是建筑,有效的设计都是工作的核心。《设计原本:计算机科学巨匠Frederick P. Brooks的思考》将对设计过程进行深入分析,揭示进行有效和优雅设计的方法。 本书包含了多个行业设计者的特别领悟。Frederick P. Brooks, Jr.精确发现了所有设计项目中内在的不变因素,揭示 了进行优秀设计的过程和模式。通过与几十位优秀设计者的对话,以及他自己在几个设计......一起来看看 《设计原本》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具