绑定对R值的引用 [英] Binding reference to an R-value
问题描述
我们都知道绑定
const的引用有特殊规则。到一个R值,即:
int const& i = 5;
表现得好像:
int const __rval = 5;
int const& i = __rval;
考虑以下函数:
int const& Func()
{
int i = 7;
返回i;
}
我们看到引用绑定到return语句中的L值,
所以绑定对const到R值的引用规则不适用。必须将
返回语句重写为以下内容吗?
返回(int)i;
按顺序强制执行绑定引用const的规则到R-
值?或者功能注定要失败?
这是一个演示程序:
#include< iostream>
#include< ostream>
使用std :: cout;
使用std :: endl;
int const& Func()
{
int i = 7;
return(int) i;
}
int main()
{
cout<< Func()<<结束;
}
-
Frederick Gotham
We all know that there''s special rules for the binding of a "reference to
const" to an R-value, i.e.:
int const &i = 5;
acts as if it were:
int const __rval = 5;
int const &i = __rval;
Consider the following function:
int const &Func()
{
int i = 7;
return i;
}
We see that the reference is bound to an L-value in the return statement,
and so the "bind reference to const to R-value" rules don''t apply. Must the
return statement be rewritten as the following?
return (int)i;
in order to enforce the rules for binding a "reference to const" to an R-
value? Or is the function just destined to fail?
Here''s a program to demonstrate:
#include <iostream>
#include <ostream>
using std::cout;
using std::endl;
int const &Func()
{
int i = 7;
return (int)i;
}
int main()
{
cout << Func() << endl;
}
--
Frederick Gotham
推荐答案
Frederick Gotham写道:
Frederick Gotham wrote:
我们都知道绑定引用的特殊规则br />
const"到一个R值,即:
int const& i = 5;
表现得好像:
int const __rval = 5;
int const& i = __rval;
考虑以下函数:
int const& Func()
{
int i = 7;
返回i;
}
我们看到引用绑定到return语句中的L值,
所以绑定对const到R值的引用规则不适用。必须将
返回语句重写为以下内容吗?
返回(int)i;
按顺序强制执行绑定引用const的规则到R-
值?或者功能注定要失败?
这是一个演示程序:
#include< iostream>
#include< ostream>
使用std :: cout;
使用std :: endl;
int const& Func()
{
int i = 7;
return(int) i;
}
int main()
{
cout<< Func()<<结束;
}
-
Frederick Gotham
We all know that there''s special rules for the binding of a "reference to
const" to an R-value, i.e.:
int const &i = 5;
acts as if it were:
int const __rval = 5;
int const &i = __rval;
Consider the following function:
int const &Func()
{
int i = 7;
return i;
}
We see that the reference is bound to an L-value in the return statement,
and so the "bind reference to const to R-value" rules don''t apply. Must the
return statement be rewritten as the following?
return (int)i;
in order to enforce the rules for binding a "reference to const" to an R-
value? Or is the function just destined to fail?
Here''s a program to demonstrate:
#include <iostream>
#include <ostream>
using std::cout;
using std::endl;
int const &Func()
{
int i = 7;
return (int)i;
}
int main()
{
cout << Func() << endl;
}
--
Frederick Gotham
为了从Func返回引用,您必须将引用
返回到Func返回后存在的内容。在堆栈上定义的变量
堆栈不满足此要求。
一个例子是经典的Meyer'的单身人士:
>
int const& Func()
{
static int i = 7;
返回i;
}
还有其他人,但我不知道你的要求是什么。
In order to return a reference from Func, you must return a reference
to something that exists after Func returns. Variables defined on
the stack do not satisfy this requirement.
One example which is the classic Meyer''s singleton:
int const & Func ()
{
static int i = 7;
return i;
}
There are others, but I don''t know what your requirements are.
一个********** @ gmail.com 张贴:
An**********@gmail.com posted:
为了从Func返回引用,您必须将引用
返回到Func返回后存在的内容。
In order to return a reference from Func, you must return a reference
to something that exists after Func returns.
已确认。
Acknowledged.
堆栈上定义的变量不满足此要求。
Variables defined on the stack do not satisfy this requirement.
已确认。
Acknowledged.
一个例子是经典的迈耶单身人士:
int const& Func()
{
static int i = 7;
返回i;
}
还有其他人,但我不知道你的要求是什么。
One example which is the classic Meyer''s singleton:
int const & Func ()
{
static int i = 7;
return i;
}
There are others, but I don''t know what your requirements are.
原帖中的代码解释了。
-
Frederick Gotham
The code in my original post explained.
--
Frederick Gotham
Frederick Gotham写道:
Frederick Gotham wrote:
>
我们都知道''关于绑定引用
const的特殊规则到一个R值,即:
int const& i = 5;
表现得好像:
int const __rval = 5;
int const& i = __rval;
考虑以下函数:
int const& Func()
{
int i = 7;
返回i;
}
我们看到引用绑定到return语句中的L值,
所以绑定对const到R值的引用规则不适用。必须
将返回语句改写如下?
返回(int)i;
按顺序强制执行绑定引用const的规则到R-
值?或者功能注定要失败?
>
We all know that there''s special rules for the binding of a "reference to
const" to an R-value, i.e.:
int const &i = 5;
acts as if it were:
int const __rval = 5;
int const &i = __rval;
Consider the following function:
int const &Func()
{
int i = 7;
return i;
}
We see that the reference is bound to an L-value in the return statement,
and so the "bind reference to const to R-value" rules don''t apply. Must
the return statement be rewritten as the following?
return (int)i;
in order to enforce the rules for binding a "reference to const" to an R-
value? Or is the function just destined to fail?
[snip]
我认为
返回int(i) ;
应该没问题。据我所知,int(i)将构造一个临时的int
r值,它与const int& ;.很好地绑定。由于我不知道什么是B级演员表演的黑魔法,我无法发表评论
return(int)i;
Best
Kai-Uwe Bux
[snip]
I think
return int(i);
should be fine. As far as I can see, int(i) will construct a temporary int
r-value that binds nicely to the const int &. Since I have no idea what
kind of black magic a C-style cast performs, I cannot comment on
return (int)i;
Best
Kai-Uwe Bux
这篇关于绑定对R值的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!