Re: Generic C++ templates/library for FreeBSD base

From: Gleb Popov <arrowd_at_freebsd.org>
Date: Fri, 14 Mar 2025 04:19:04 UTC
On Thu, Mar 13, 2025 at 11:53 PM John Baldwin <jhb@freebsd.org> wrote:
>
> One is a stringf() function that accepts printf() style format string and
> arguments and returns a std::string.  I know C++23 adds <format>, but we
> can't assume that yet, and this function is probably more useful when
> adapting existing C code.  Compared to some other solutions, I chose to
> wrap asprintf() and do an extra copy at the end into a std::string rather
> than calling vsnprintf() twice.  It seems less ugly than the vsnprintf()
> solutions also:
>
> https://github.com/freebsd/freebsd-src/commit/01bd3d89ddf9ccbf884e52fe7289e8a9278e2d63

I wonder why std::string always copies the data passed into the constructor.

It is possible to avoid extra alloc by returning a std::string_view,
but this would force the caller into freeing the memory manually.
Maybe derive from it and extend the destructor, but this just brings
me to the initial question.

> The other is a FILE_up that wraps a FILE * and uses fclose() as its deleter
> buried as part of a larger change:
>
> https://github.com/freebsd/freebsd-src/commit/77b800022b18ab61983b22022b16943355b2bf75

I see the code uses exceptions. Was it a conscientious decision?