64,654
社区成员
发帖
与我相关
我的任务
分享
5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote]
没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用
讲引用的地方有
A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
[/quote]
用const引用来引用临时变量,是标准提倡的,还是标准规定必须这么做的?[/quote]必须的[/quote]这个应该属于英语范畴
如果不确定shall的意思,可以去查词典
(通常指按照规则或法律规定)必须,一定,将5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote]
没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用
讲引用的地方有
A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
[/quote]
用const引用来引用临时变量,是标准提倡的,还是标准规定必须这么做的?[/quote]必须的5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote]
没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用
讲引用的地方有
A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
[/quote]
用const引用来引用临时变量,是标准提倡的,还是标准规定必须这么做的?5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote]
没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用
讲引用的地方有
A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote]
没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote]
++#include <string>
std::string foo(){ return "bar";}
int main(int argc,char* argv[])
{
//case one
{
std::string const& f = foo();
}
//case two
{
foo();
}
}
vc 2012 debug:
//case one
{
std::string const& f = foo();
010B44B8 lea eax,[ebp-30h]
010B44BB push eax
010B44BC call foo (010B11EFh)
010B44C1 add esp,4
010B44C4 lea ecx,[ebp-30h]
010B44C7 mov dword ptr [ebp-0Ch],ecx
}
010B44CA lea ecx,[ebp-30h]
010B44CD call std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (010B12B7h)
//延长到}
//case two
{
foo();
010B44D2 lea eax,[ebp-114h]
010B44D8 push eax
010B44D9 call foo (010B11EFh)
010B44DE add esp,4
010B44E1 lea ecx,[ebp-114h]
010B44E7 call std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (010B12B7h)
//立即析构
}
}
5 The second context is when a reference is bound to a temporary. The temporary to which the reference is
bound or the temporary that is the complete object of a subobject to which the reference is bound persists
for the lifetime of the reference except:
— A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of
the full-expression containing the call.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
const 修饰表示只读数据段, 与生命周期无关的
只读数据段仅是实现的其中一种方式,目的是在运行时令试图修改const变量的行为产生错误。
由于C标准并没有禁止对const变量的修改,而是规定属于未定义行为,
因此一个实现对于试图修改const的行为如何处理都没有违反标准,也就是说,
无论把const变量放在只读段也好,放在可被修改的地方也好(例如你说的栈),都是允许的行为。