【发布时间】:2017-05-05 13:36:57
【问题描述】:
我遇到过这种模式的多个实例(仅以 boost::filesystem 为例):
boost::filesystem::path path = ...;
someFunctionTakingCStrings(path.string().c_str());
在哪里
const std::string path::string() const
{
std::string tmp = ...
return tmp;
}
虽然我从未遇到过这种模式的问题,但我想知道sting() 返回的字符串何时被销毁,以及访问c_str() 的代码是否像c_str() lifetime is bound to std::string lifetime 一样安全。
【问题讨论】:
-
为什么它被标记为 C 问题?
-
因为里面有 c_str()
-
@PhilLab: 但这并不意味着它会在 C 中编译 ;-)