C++ 引用无处 [英] C++ reference to nowhere
问题描述
想到这个问题为什么我可以在初始化数据成员之前用数据成员初始化引用成员?以及这个为什么初始化新变量本身有效? 我想到了以下奇怪的代码:
Thinking of this question Why can I initialize reference member with data member before initializing the data member? as well as this Why is initialization of a new variable by itself valid? following strange code came to my mind:
int main()
{
int &ri = ri;
ri = 0;
}
此代码编译现场演示,我知道这是UB,但只是想知道,编译器内部会发生什么,这个引用实际上指向哪里?
This code compiles live demo, I know this is UB, but just wonder, what happens inside compiler, where this reference actually points to?
更新:当引用是全局的时会发生不同的情况:godbolt,gcc 好像把它声明为一个指针并让它指向自己
Update: different situation happens when that reference is global: godbolt, gcc seems to declare it as a pointer and make it to point to itself
downvoters 的注意事项:我知道这段代码是无效的,我不期待编译器的特定行为,但我认为查看内部可能有助于理解为什么以下代码不非法以及引用如何在语言中工作:
Note for downvoters: I know this code is invalid and I am not expecting particular behavior from a compiler, but I think looking inside may help to understand why following code is not illegal and how references work in the language:
struct foo {
int &ri = i;
int i = 0;
};
或者这个:
extern int i;
int &ri = i;
int i = 0;
更新2:虽然赋值绝对是UB,但如果声明本身,这是一个好问题:
Update2: while assignment is definitely UB, it is a good question, if declaration itself:
int &ri = ri;
是不是UB.这似乎是纯粹的邪恶 - ri
不能以任何方式使用,但声明本身似乎是有效的.
is UB or not. This seem to be is pure evil - ri
cannot be used in any way, but declaration itself seems to be vaild.
推荐答案
不是答案,只是长评论.
Not an answer, just a long comment.
我不知道您所说的是什么意思,...它不会在没有强制转换的情况下在 C++ 上编译...".
引用必须如何实现,据我所知没有具体说明.然而,它们必须是伪装的指针(它们还能是什么?).参考来源 和 指针源 将生成完全相同的代码.
How references must be implemented, it is not specified as far as I know. However, they must be pointers in disguise (what else could they be?). The references source and the pointers source will generate exactly the same code.
...根本不必在内存中表示,..." 我不确定我是否理解这意味着什么.引用的地址是它引用的对象的地址.因此,您可能会说引用本身没有地址.以下将打印i
的地址两次:
"...does not have to be represented in memory at all,..." I am not sure I understand what this means. The address of a reference is the address of the object it refers to. Therefore you might say the reference itself has no address. The following will print i
's address twice:
int i;
int &ri = i;
cout << &ri << endl << &i;
由于语言允许使用未初始化的变量,我认为一个好的编译器必须发出警告而不是错误:使用未初始化的变量
.
Since the language allows the use of uninitialized variables, I think a good compiler must issue an warning but not an error: uninitialized variable used
.
我和一个参与 C++ 设计的人进行了一次简短的讨论(我忘了征得他的同意).简而言之,禁止 T x = x;
是可取的,但 C++ 不是从头开始设计的:它必须向后兼容 C.
I had a short discussion with someone involved in the design of C++ (I forgot to ask permission to name him). In short, banning T x = x;
is desirable, but C++ was not designed from scratch: it had to be backward compatible with C.
因此,您找到了一种合法方法来创建未初始化的引用....这个引用实际上指向哪里?"的答案是:一个未初始化的指针指向哪里.
So, you found a legal way to create an uninitialized reference. The answer to "...where this reference actually points to?" is: where an uninitialized pointer points.
这篇关于C++ 引用无处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!