简体   繁体   中英

Does std::istringstream::imbue own the object passed

I'm using Boost to convert a date of the form "01-Jan-2000" to a julian number. The way I do this is to use

int toJulian(std::string date)
{
    std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%d-%b-%Y"));
    std::istringstream ss(date);
    ss.imbue(loc);
    boost::posix_time::ptime p;
    ss >> p;
    return p.date().julian_day();
}

(This is along the lines of the examples in the Boost documentation).

But it's not clear to me if this leaks memory or not. I don't have an explicit delete . Obviously if imbue passes ownership of the pointer in loc to the stream then perhaps it's deleted when ss goes out of scope.

Am I correct?

See http://www.boost.org/doc/libs/1_43_0/doc/html/date_time/date_time_io.html#date_time.format_flags

Short answer: No, but the std::locale object does .

You want to be looking at http://en.cppreference.com/w/cpp/locale/locale/locale

You are calling the constructor (overload 7)

template< class Facet >
locale( const locale& other, Facet* f );

The linked reference is clear:

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.

So yes, something will delete the object for you, but it's actually the std::locale instance that does it, not the stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM