On Fri, 2018-02-16 at 07:14 -0500, Trevor Saunders wrote: > On Thu, Feb 15, 2018 at 04:04:45PM -0500, David Malcolm wrote: > > On Fri, 2018-02-09 at 13:01 +0000, Nick Clifton wrote: > > > +class escaped_string > > > +{ > > > + public: > > > + escaped_string () { m_owned = false; m_str = NULL; }; > > > + ~escaped_string () { if (m_owned) free (m_str); } > > > + operator const char *() const { return (const char *) m_str; } > > > + void escape (const char *); > > > + private: > > > + char * m_str; > > > + bool m_owned; > > > +}; > > > > I'd hoped that instead of this we could have an escape_string > > function > > return an instance of a class that has responsibility for the "if > > (ownership) free" dtor, but I don't think C++89 supports that (I > > had a > > go at implementing it, but I think we'd need C++11's move > > semantics, > > rather than just the NRVO). > > I think either gcc::unique_ptr does what you want here,
That would involve a second heap allocation; as-is, the object lives on the stack. > or you could > use the same trick of having a copy ctor that takes a non constant > reference to implement this in C++98. Maybe. I was trying not to bash Nick over the head too much with C++ here :) If you want to implement it, go for it (I can think of possible uses for it in the driver, for fixing leaks there when used by libgccjit). > Thanks! > > Trev