所有权/执行删除操作的面在一个区域(的std ::区域) [英] Ownership/delete'ing the facet in a locale (std::locale)

查看:161
本文介绍了所有权/执行删除操作的面在一个区域(的std ::区域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的函数用来得到一个日期/时间字符串的Boost.Date_Time

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();
}

我对所有权/ 删除'的面的ING 对象一个快速的问题。 的std ::区域的构造是不是所有权/ 删除明确'的面的ING 。尝试使用的shared_ptr -wrapped和堆栈分配版本 - 这两者造成赛格故障。此外,通过运行Valgrind的上述功能并没有表​​现出任何泄漏(这可能意味着区域或流照顾的删除'ING),但我只是想明确的是,我在这里做正确的事。谢谢你。

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.

推荐答案

根据 Stroustrup的,0参数传递给构造告诉区域设置将处理的破坏,以及两者构造 BPT :: time_facet 默认为0时不提供它。非零值,虽然,意味着程序员必须明确地处理面的破坏

According to Stroustrup, a 0 argument passed to the constructor tells the facet that the locale will handle destruction, and the both constructors of bpt::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 the facet.

这篇关于所有权/执行删除操作的面在一个区域(的std ::区域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆