所有权/删除区域设置中的方面(std::locale)
我编写了以下函数来使用 boost 获取日期/时间字符串。日期_时间。
namespace bpt = boost::posix_time;
string
get_date_time_string(bpt::ptime time)
{
bpt::time_facet * facet(new bpt::time_facet);
facet->format("%Y%m%d%H%M%S");
stringstream return_value;
return_value.imbue(std::locale(std::locale::classic(), facet));
return_value << time;
return return_value.str();
}
我有一个关于facet
对象的所有权/删除
的快速问题。 std::locale 的构造函数 在所有权上不明确/删除
facet
。尝试使用shared_ptr
包装和堆栈分配版本的facet
- 这两者都会导致段错误。另外,通过 valgrind 运行上述函数没有显示任何泄漏(这可能意味着语言环境或流正在处理删除),但我只是想清楚我正在做正确的事情在这里。谢谢。
I wrote the following function to get a date/time string using boost.date_time.
namespace bpt = boost::posix_time;
string
get_date_time_string(bpt::ptime time)
{
bpt::time_facet * facet(new bpt::time_facet);
facet->format("%Y%m%d%H%M%S");
stringstream return_value;
return_value.imbue(std::locale(std::locale::classic(), facet));
return_value << time;
return return_value.str();
}
I had a quick question about the ownership/delete
'ing of the facet
object. std::locale's constructor is not explicit on the ownership/delete
'ing of the facet
. Tried using shared_ptr
-wrapped and stack allocated versions of facet
- both of which caused seg-faults. Also, running the above function through valgrind didn't show any leaks(which probably implies that the locale or stream is taking care of delete
'ing), but I just wanted to be clear that I am doing the right thing here. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Stroustrup,传递给构造函数的 0 参数告诉
facet
locale
将处理销毁,并且bpt::time_facet
的两个构造函数在未提供时默认为 0。不过,非零值意味着程序员必须显式处理facet
的销毁。According to Stroustrup, a 0 argument passed to the constructor tells the
facet
that thelocale
will handle destruction, and the both constructors ofbpt::time_facet
default to 0 when it isn't supplied. A non-zero value, though, implies that the programmer must explicitly handle the destruction of thefacet
.