为什么函数指针可以是`constexpr`? [英] Why can function pointers be `constexpr`?

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

问题描述

在执行程序之前,编译器如何知道平方根在内存中的哪个位置?我以为每次执行程序时地址都会不同,但这可以起作用:

How does the compiler know where in memory the square root will be before the program is executed? I thought the address would be different everytime the program is executed, but this works:

constexpr double(*fp)(double) = &sqrt;
cout << fp(5.0);

是因为地址相对于内存中的另一个地址吗?我不这样认为,因为 fp 的值很大:0x720E1B94。

Is it because the address is relative to another address in memory? I don't think so because the value of fp is large: 0x720E1B94.

推荐答案


编译器如何在执行程序之前知道平方根在内存中的什么位置?

How does the compiler know where in memory the square root will be before the program is executed?

工具链可以决定将函数放置在何处。

The tool chain gets to decide where it puts the functions.


是因为地址相对于内存中的另一个地址吗?

Is it because the address is relative to another address in memory?

如果产生的程序是可重定位位置独立,是的,那就是案子。如果程序都不是,则地址甚至可以是绝对地址。

If the produced program is either relocatable or position independent then yes, that's the case. If the program is neither, then the address can even be absolute.


为什么下一次程序执行时,完全相同的内存点可用

Why would the exact same memory spots be available next time the program is run?

因为内存空间是虚拟

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

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