5
让我们来看看这两个功能:如果没有const引用它,是否会删除临时对象?
std::string get_string()
{
std::string ret_value;
// Calculate ret_value ...
return ret_value;
}
void process_c_string(const char* s)
{
std::cout << s << endl;
}
这里是process_c_string
与get_string
返回参数两种可能的调用。
没有绑定const引用返回的对象
get_string
。process_c_string(get_string().c_str());
随着常量引用的
get_string
返回的对象绑定。const std::string& tmp_str = get_string(); process_c_string(tmp_str.c_str());
我知道,第二个方法是有效的,但对于第一个,是什么标准说的这种情况? get_string
返回的临时对象会在process_c_str
之前被删除,因为它没有const reference
吗?
注意:这两个版本都可以在MSVC中。