关于const reference的几点说明

博主在程序中发现将引用指向不同类型的代码,经网上查询得知,只要存在可行转换路径,编译器构造引用时会创建临时变量,让引用指向它。非const reference无此待遇,const reference会增强函数适用性,但可能有开销,且const只是编译期概念。
几天前,在程序中遇到一个问题,当我检查程序错误时,在STL实现中发现了类似下面的代码:
#include <iostream>
using namespace std;

class
 A
{

public
:
    A(const string& s)
    {

        cout << s.c_str() << endl;
    }
};


int
 main()
{

    const
 char* psz = "abc";
    A a(psz);

    return
 0;
}

对此,我感到很奇怪,因为我从来没有这样做过,怎么能把一个引用指向一个与引用本身类型不同的类型呢?于是,我在网上发了一个帖子,很快,有人从CPL 3rd中摘录了以下内容作为回复:

// Begin
Initialization of a reference is trivial when the initializer is an 
lvalue (an object whose address you can take; see §4.9.6). The 
initializer for a 'plain' T& must be an lvalue of type T. The 
initializer for a const T& need not be an lvalue or even of type T. 
In such cases (David''s notes: and only in such cases),
[
1] first, implicit type conversion to T is applied if necessary (see §C.6);
[
2] then, the resulting value is placed in a temporary variable of type T; and
[
3] finally, this temporary variable is used as the value of the initializer.
Consider:
double
& dr = 1; // error: lvalue needed
const double& cdr = 1; // ok
The interpretation of this last initialization might be:
double
 temp = double(1) ; // first create a temporary with the right value
const double& cdr = temp; // then use the temporary as the initializer for cdr
A temporary created to hold a reference initializer persists until 
the end of its reference’s scope.
References to variables and references to constants are distinguished 
because the introduction of a temporary in the case of the variable 
is highly errorprone;
an assignment to the variable would become an assignment to the – 
soon to disappear – temporary. No such problem exists for references 
to constants, and references to constants are often important as 
function arguments (§11.6).
// End

也就是说,只要存在可行的转换路径,编译器在构造引用的时候会创建一个与引用类型类型信息相同的临时变量,然后,用引用指向该临时变量.同时,上面这段文字还给了我们如下信息:
1.
const reference不会享受这样的特殊待遇.如下面的代码是错误的:
float
 f = 0;
double
& d = f;

char
* psz = "abc";
string& rstr = psz;
(
: VC6在编译以上各句时报告了一个与实际不符的错误提示:A reference that is not to 'const' cannot be bound to a non-lvalue,这里的f和psz明明是左值)
2.
const reference会使函数的适用性更强,但代价是在进行类型转换时可能引入较大的开销,失去引用传递的高效性.

当然,除此之外,const只是一个编译期的概念,public/protected/private等一样,只是告诉编译器,我们需要针对这些标示之后的东西添加一些附加的描述,请区别对待.这些描述对于private而言,是说:进行语法检查时如果发现非本类或friend访问了这些成员(方法),则报告错误;对于const而言,则是说:如果语法检查时发现有任何操作对const所修饰的对象进行了修改,则报告错误.
在语法检查之后,这些修饰符就该悄然引退了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值