为什么允许指向内联函数的指针? [英] Why are pointers to inline functions allowed?

查看:138
本文介绍了为什么允许指向内联函数的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:

1)为什么C ++中允许使用指向内联函数的指针? 我读过内联函数的代码只是复制到函数调用语句中,并且内联函数中没有编译时内存分配.那么,既然内联函数没有固定的内存地址,为什么为什么要有一个指向内联函数的指针呢?

1) Why are pointers to inline functions allowed in C++? I have read that the code of inline functions just gets copied to the function call statement and there is no compile-time memory allocation in inline functions. So why can a pointer exist to an inline function, given that there is no fixed memory address for inline functions?

2)考虑以下代码:

inline void func()    
{
    int n=0;
    cout<<(&n);
} 

是否每次调用func()时都不会打印n地址的不同值? [因为我认为每次复制内联函数代码时,都必须重新分配局部变量(而在普通函数的情况下,会进行重新初始化)]

Should it not print different values of the address of n each time func() is called? [Because I think that every time inline function code is copied, reallocation of the local variables must be done (whereas in the case of normal functions, reinitialisation takes place)]

我是一个初学者,为了加强我的概念,我问了这个问题.如果我在任何地方错了,请纠正我.

I am a beginner and I asked this question for the sake of my concept strengthening. Please correct me if I am wrong anywhere.

推荐答案

1)为什么在c ++中允许使用指向内联函数的指针?

1) Why pointers to inline functions are allowed in c++?

因为内联函数是与其他函数一样的函数,所以指向它们是函数可以执行的操作之一.内联函数在这方面并不特殊.

Because inline functions are functions just like any other, and pointing to them is one of the things that you can do with functions. Inline functions just aren't special in this regard.

我读到内联函数的代码只是复制到函数调用语句中,并且内联函数中没有编译时内存分配.

I have read that code of inline functions just get copied to the function calling statement and there is no compile time memory allocations in inline functions.

您(也许您已阅读的材料)混合了两个相关且名称相似的概念.

You (and perhaps the material you've read) have mixed two related and similarly named concepts.

在所有使用内联函数的翻译单元中都定义了一个内联函数,而仅按一个定义规则的要求在一个翻译单元中定义了一个非内联函数.这就是函数的内联声明的含义;它放宽了一个定义规则,但是还提出了在使用它的所有翻译单元中进行定义的附加要求(如果不放宽odr,就不可能实现).

An inline function is defined in all translation units that use it, while a non-inline function is defined in one translation unit only as required by the one definition rule. That is what an inline declaration of a function means; it relaxes the one definition rule, but also gives the additional requirement of being defined in all translation units that use it (which would not have been possible if the odr wasn't relaxed).

内联扩展(或内联)是一种优化,其中通过将被调用的函数复制到调用者的框架中来避免函数调用.无论函数是否已声明为内联,都可以内联扩展函数调用.而且,已声明为内联的函数不必扩展为内联.

Inline expansion (or inlining) is an optimization, where a function call is avoided by copying the called function into the frame of the caller. A function call can be expanded inline, whether the function has been declared inline or not. And a function that has been declared inline is not necessarily expanded inline.

但是,不能在未定义功能的转换单元中内联扩展功能(除非链接时间优化执行扩展).因此,内联声明允许在所有TU中定义的要求,通过允许在调用它的所有TU中定义函数,也使得函数的内联扩展成为可能.但是无法保证优化.

However, a function can not be expanded inline in a translation unit where it is not defined (unless link time optimization performs the expansion). Therefore the requirement of being defined in all TUs that the inline declaration allows, also makes possible the inline expansion of the function by allowing the function to be defined in all TUs that invoke it. But the optimization is not guaranteed.

2)是否不应该在每次调用func()时打印n的不同地址值?

2) Should it not print different values of address of n each time func() is called?

内联扩展确实会导致局部变量位于调用方的框架中,是的.但是,如果呼叫来自不同的帧,则无论扩展如何,它们的位置都会有所不同.

Inline expansion does cause the local variables to be located in the frame of the caller, yes. But their location will differ regardless of expansion if the calls originate from separate frames.

通常会针对已内联扩展的任何函数生成一个常规的非扩展版本.如果使用一个函数的地址,它将指向该未扩展的函数.如果编译器可以证明对函数的所有调用都是内联的,则编译器可能选择完全不提供未扩展的版本.这要求该功能具有内部链接,并且采用该功能的地址通常会使这种证明非常困难或不可能.

There is typically a regular non-expanded version generated of any function that has been expanded inline. If the address of a function is taken, it will point to that non-expanded function. If the compiler can prove that all calls to a function are inlined, the compiler might choose to not provide the non-expanded version at all. This requires that the function has internal linkage, and taking the address of the function typically makes such proof very difficult, or impossible.

这篇关于为什么允许指向内联函数的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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