std::string::c_str() and temporaries(std::string::c_str() 和临时变量)
问题描述
以下 C++ 代码格式是否正确:
Is the following C++ code well-formed:
void consumer(char const* p)
{
std::printf("%s", p);
}
std::string random_string_generator()
{
// returns a random std::string object
}
consumer(random_string_generator().c_str());
我遇到的问题是,在创建临时 std::string 对象并获取 c_str() 指针后,没有什么可以阻止 std::string 对象被破坏(或者我可能错了?).如果代码没问题的话,你能不能给我指出标准.当我用 g++ 测试时,它确实有效.
The problem I have with it is, that after creating the temporary std::string object and taking the c_str() pointer, nothing prevents the std::string object from getting destroyed (or maybe I'm wrong?). Can you please point me to the standard, if the code is OK despite everything. It does work, when I test with g++.
推荐答案
std::string::c_str()返回的指针指向内存由字符串对象维护.它一直有效,直到非常量在字符串对象上调用函数,或者字符串对象是毁坏了.您关心的字符串对象是临时对象.它将在完整表达式的末尾销毁,而不是在之前和不是之后.在您的情况下,完整表达式的结尾在调用 consumer,所以你的代码是安全的.如果 consumer将指针保存在某处,以备后用.
The pointer returned by std::string::c_str() points to memory
maintained by the string object. It remains valid until a non-const
function is called on the string object, or the string object is
destructed. The string object you're concerned about is a temporary.
It will be destructed at the end of the full expression, not before and
not after. In your case, the end of the full expression is after the
call to consumer, so your code is safe. It wouldn't be if consumer
saved the pointer somewhere, with the idea of using it later.
从 C++98 开始就严格定义了临时对象的生命周期.在此之前,它会有所不同,具体取决于编译器和您编写的代码编写的不会与 g++ 一起工作(1995 年之前,粗略地—g++当标准委员会投票时几乎立即改变了这一点).(那时也没有 std::string,但同样的问题会影响任何用户编写的字符串类.)
The lifetime of temporaries has been strictly defined since C++98.
Before that, it varied, depending on the compiler, and the code you've
written wouldn't have worked with g++ (pre 1995, roughly—g++
changed this almost immediately when the standards committee voted it).
(There wasn't an std::string then either, but the same issues affect
any user written string class.)
这篇关于std::string::c_str() 和临时变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::string::c_str() 和临时变量
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
