libutil: add shim to impl operator<< in terms of std::format

Change-Id: Ie749801f669c0cfcd93b8ff559add96a6a6a6964
This commit is contained in:
Qyriad
2025-12-13 15:07:39 +01:00
parent 9d6e71bd56
commit 43b1b63df9
+24
View File
@@ -4,6 +4,7 @@
#include <iostream>
#include <string>
#include <boost/format.hpp>
#include <format>
#include "lix/libutil/ansicolor.hh"
// Explicit instantiation in fmt.cc
@@ -11,6 +12,29 @@ extern template class boost::basic_format<char>;
namespace nix {
/** Sentinel template struct to mark if a type should automatically generate
* an ostream operator<< based on std::format().
*
* This template should be specialized to inherit from @ref std::true_type for any type
* that has an @ref std::formatter<> implementation, but not an operator<< implementation.
*/
template<typename T>
struct LegacyFormat : std::false_type
{};
/** Automatically generated ostream operator<< based on @ref std::formatter<> impls.
*
* Only enabled for `T` where @ref std::formatter<T> is specialized,
* @ref nix::LegacyFormat<T>::value is true-ish.
*/
template<std::formattable<char> T>
requires LegacyFormat<T>::value
std::ostream & operator<<(std::ostream & out, T const & self)
{
out << std::format("{}", self);
return out;
}
/** Gets a C++ stack trace using boost stacktrace */
std::string getStackTrace();