简体   繁体   English

在C ++中延长临时值的生命周期

[英]Extending the lifetime of a temporary value in C++

const int &ra=3;
  1. As I know, making ra const will extends the lifetime of the temporary r-value which is in this case 3. This is a little bit confusing, as I know ra should have the same address as r-value which is here 3, but 3 is not a real variable and it does not have a memory where it's stored. 正如我所知,制作ra const将延长临时r值的生命周期,在这种情况下为3.这有点令人困惑,因为我知道ra应该具有与r值相同的地址,这是3,但是3不是一个真正的变量,它没有存储它的存储器。 So how can this be possible? 那怎么可能呢?

  2. what is the difference between: 有什么区别:

     const int& ra=a; 

    and

     int& const ra=a; 

but 3 is not a real variable and it does not have a memory where it's stored. 但是3不是一个真正的变量,它没有存储它的存储器。 so how can this be possible ? 那怎么可能呢?

Actually a temporary object gets created out of the literal 3 , and then that temporary is bound to the const reference. 实际上,临时对象是从文字3创建的,然后该临时对象绑定到const引用。 That is how it becomes possible. 这就是它成为可能的方式。


Now your next question: the difference between these two 现在你的下一个问题是:这两者之间的区别

const int& ra=a;
int& const ra=a;

is that the second statement is illegal. 是第二个声明是非法的。

1) Whether or not the compiler decides to actually store your number 3 is a detail subject to optimisation decisions. 1)编译器是否决定实际存储您的数字3是一个受优化决策影响的细节。 As far as the language is concerned, the temporary object lives as long as the reference. 就语言而言,临时对象与引用一样长。 Practically, if you only need the value (not the object ), the object may never be stored at all, and instead the compiler may substitute the value directly whenever you use ra . 实际上,如果您只需要 (而不是对象 ),则根本不会存储该对象,而是编译器可以在您使用ra时直接替换该值。 Of course, if you take the address (via &ra ), then the compiler will make sure that an object is actually stored somewhere so that you can take its address. 当然,如果您获取地址(via &ra ),则编译器将确保某个对象实际存储在某处,以便您可以获取其地址。 (It may still substitute the value directly elsewhere, rather than loading it from that address.) (它可能仍然直接在其他地方替换值,而不是从该地址加载它。)

2) The second version isn't valid C++. 2)第二个版本无效C ++。 You can only say int const & x and const int & x , for the same reason that int const and const int denote the same type. 你只能说int const & xconst int & x ,原因与int constconst int表示相同的类型相同。 The reference itself has no notion of constancy; 引用本身没有恒定的概念; it is always bound to the object with which it is initialized (ie you can't have a "naked" reference object int & x; ). 它总是绑定到初始化它的对象(即你不能有一个“裸”引用对象int & x; )。

From my understanding, when you make a variable const, it's allocated a memory where it's stored, like any other variable. 根据我的理解,当你创建一个变量const时,它会分配一个存储它的内存,就像任何其他变量一样。 Depending on the compiler, if you place that const 3 in a function, it might allocate a temp 3 every time you call the function, or it might just reference a static value allocated when the program started. 根据编译器的不同,如果将const 3放在一个函数中,它可能会在每次调用函数时分配一个temp 3,或者它可能只引用程序启动时分配的静态值。 That 3 is a real variable in that it's data that's been stored somewhere, just like every other variable. 这是一个真正的变量,因为它的数据存储在某处,就像其他变量一样。

However, referencing that value outside of its scope is going to be a crapshoot as to whether or not it will work, and so you shouldn't use that as a way to "extend the life" of a variable. 但是,在其范围之外引用该值将是一个关于它是否可行的废话,因此您不应该将其用作“延长变量寿命”的方法。 You might get lucky and have the const be allocated in a static place, and so you'll always get the value you want, even when you access it out of scope; 你可能会很幸运,并且const会被分配到一个静态的地方,所以你总能获得你想要的值,即使你在范围之外访问它也是如此; but you might get unlucky, and end up grabbing data that's written over the temp value you wanted. 但是你可能会感到不幸,并最终抓住写在你想要的临时值上的数据。 The behavior is "undefined" - that means compilers decide how they want to do it, so there's no way you can count on. 行为是“未定义的” - 这意味着编译器决定他们想要怎么做,所以你无法依靠它。 Instead, increase the scope of the variable by declaring it earlier, or allocating it on heap ( int* x = malloc(sizeof(int))//, etc. and passing its pointer around. 相反,通过先声明它来增加变量的范围,或者在堆上分配它( int* x = malloc(sizeof(int))//, etc.并传递它的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM