Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

boost

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

like image 391
Paul Logue Avatar asked Sep 01 '25 10:09

Paul Logue


1 Answers

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.

like image 72
Bathsheba Avatar answered Sep 04 '25 00:09

Bathsheba