拥有/删除区域设置中的构面(std :: locale)

dec*_*tle 16 c++ boost locale facet boost-date-time

我编写了以下函数来使用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();
}
Run Code Online (Sandbox Code Playgroud)

我对所有权/一个简单的问题delete了的"ING facet对象.std :: locale的构造函数没有明确的所有权/ delete' facet.尝试使用shared_ptr-wrapped和堆栈分配版本facet- 两者都导致了seg-fault.此外,通过valgrind运行上述函数并没有显示任何泄漏(这可能意味着语言环境或流正在delete处理),但我只是想清楚我在这里做正确的事情.谢谢.

rco*_*yer 14

斯特劳斯,一个0参数传递给构造告诉facetlocale将处理破坏和的两个构造bpt::time_facet默认为0时不提供它.但是,非零值意味着程序员必须明确地处理破坏facet.