feat: constexpr_format added

This commit is contained in:
Mateusz Pusz
2025-06-20 13:32:29 +02:00
parent a8009febb6
commit aab013ab0a
3 changed files with 51 additions and 0 deletions

View File

@ -88,6 +88,7 @@ if(NOT ${projectPrefix}API_FREESTANDING)
BASE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/include
FILES
include/mp-units/bits/constexpr_format.h
include/mp-units/bits/fmt.h
include/mp-units/bits/format.h
include/mp-units/bits/ostream.h

View File

@ -0,0 +1,49 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <mp-units/bits/hacks.h> // IWYU pragma: keep
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <string>
#endif
#endif
namespace mp_units::detail {
[[nodiscard]] consteval std::string constexpr_format([[maybe_unused]] auto fmt, [[maybe_unused]] auto&&... args)
{
std::string text;
#if MP_UNITS_USE_FMTLIB
fmt::format_to(std::back_inserter(text), fmt, std::forward<decltype(args)>(args)...);
#else
static_assert(false, "`std::format` is not `constexpr` yet");
#endif
return text;
};
} // namespace mp_units::detail

View File

@ -33,6 +33,7 @@
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/compile.h>
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else // MP_UNITS_USE_FMTLIB