Who is responsible for deleting the facet?

后端 未结 3 1129
[愿得一人]
[愿得一人] 2020-12-28 13:59

I have a function that uses the Boost.DateTime library for generating the current GMT/UTC date and time string (live example).

std::string get_curr_date() {
         


        
3条回答
  •  庸人自扰
    2020-12-28 14:19

    Am I required to delete the time_facet or is the std::locale object responsible for deleteing the it?

    You're not required to delete the time_facet so long as time_facet derives from std::locale::facet, which it should. The std::locale::facet is a base class that all facets should derive from that implement a form of reference counting. The standard says this:

    § 22.3.1.6

    Once a facet reference is obtained from a locale object by calling use_facet<>, that reference remains usable, and the results from member functions of it may be cached and re-used, as long as some locale object refers to that facet.

    Once all references of the facet are not being used, the destructor of std::locale will manage and delete references to the facet if its ref count is 0.

    This is all specified in §22.3.1.1.2 in the C++11 standard. Where it states:

    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.

提交回复
热议问题