std::locale 取得所有权
std::locale take ownership
我有下一个代码:
boost::posix_time::time_facet facet("%Y-%m-%dT%H:%M:%f");
std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), &facet));
当我 运行 这段代码时,编译器似乎试图在 ss
析构函数调用期间释放 facet
(调试器说的)。
当我运行这段代码时:
std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%dT%H:%M:%f")));
好的。
所以,我想知道是否存在关于此行为或 local
取得所有权的原因的任何文档。我试图找出解释,但文档中没有任何内容。
std::locale()
是否拥有 time_facet?
http://en.cppreference.com/w/cpp/locale/locale/locale
template< class Facet >
locale( const locale& other, Facet* f ); (7)
Overload 7 is typically called with its second argument, f, obtained directly from a new-expression: the locale is responsible for calling the matching delete from its own destructor.
在http://eel.is/c++draft/locale.facet#3中还有另一个提示:
The refs argument to the constructor is used for lifetime management.
For refs == 0, the implementation performs delete static_cast(f) (where f is a pointer to the facet) when the last locale object containing the facet is destroyed; for refs == 1, the implementation never destroys the facet.
由此可见,facet
(以及派生的类)中存在内部引用计数机制。这或多或少意味着你传递给 locale
的 facet
是与它共享的,如果你不显式地增加它的引用计数,它将被销毁,因为传递一个指向它的指针不能增加重新计数。
我有下一个代码:
boost::posix_time::time_facet facet("%Y-%m-%dT%H:%M:%f");
std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), &facet));
当我 运行 这段代码时,编译器似乎试图在 ss
析构函数调用期间释放 facet
(调试器说的)。
当我运行这段代码时:
std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%dT%H:%M:%f")));
好的。
所以,我想知道是否存在关于此行为或 local
取得所有权的原因的任何文档。我试图找出解释,但文档中没有任何内容。
std::locale()
是否拥有 time_facet?
http://en.cppreference.com/w/cpp/locale/locale/locale
template< class Facet >
locale( const locale& other, Facet* f ); (7)
Overload 7 is typically called with its second argument, f, obtained directly from a new-expression: the locale is responsible for calling the matching delete from its own destructor.
在http://eel.is/c++draft/locale.facet#3中还有另一个提示:
The refs argument to the constructor is used for lifetime management. For refs == 0, the implementation performs delete static_cast(f) (where f is a pointer to the facet) when the last locale object containing the facet is destroyed; for refs == 1, the implementation never destroys the facet.
由此可见,facet
(以及派生的类)中存在内部引用计数机制。这或多或少意味着你传递给 locale
的 facet
是与它共享的,如果你不显式地增加它的引用计数,它将被销毁,因为传递一个指向它的指针不能增加重新计数。