大家都知道const&类型参数与&类型参数的区别是能否通过形参更改实参
在传送什么样子的参数上面二者还有一点区别,举个例子
void generate_func(string& s){
cout<<"this is generate"<<endl;
}
void const_func(const string& s){
cout<<"this is const"<<endl;
}
const_func("sssss");编译通过
generate_func("sssssss");编译错误
无论哪个调用都使用了隐式转换,将字面值转换为string ,c++不允许将临时量绑定到&形参上面,具体到这里就是string&。
至于为什么不允许呢,没有这个必要罢了。假设允许的话,也没有什么用途,绑定到&上面的是一个临时量,失去了&变量的本意,如此c++就将这种情况列为非法