Skip to content

Commit 7e7a4e9

Browse files
committed
Enable constexpr support for fmt::format (#3403)
1 parent 3ba3c39 commit 7e7a4e9

3 files changed

Lines changed: 83 additions & 8 deletions

File tree

include/fmt/compile.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace detail {
4242
#endif
4343

4444
template <typename T, typename... Tail>
45-
auto first(const T& value, const Tail&...) -> const T& {
45+
constexpr auto first(const T& value, const Tail&...) -> const T& {
4646
return value;
4747
}
4848

@@ -436,8 +436,8 @@ FMT_BEGIN_EXPORT
436436
template <typename CompiledFormat, typename... Args,
437437
typename Char = typename CompiledFormat::char_type,
438438
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
439-
FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
440-
const Args&... args) {
439+
FMT_INLINE FMT_CONSTEXPR_STRING std::basic_string<Char> format(
440+
const CompiledFormat& cf, const Args&... args) {
441441
auto s = std::basic_string<Char>();
442442
cf.format(std::back_inserter(s), args...);
443443
return s;
@@ -452,8 +452,8 @@ constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf,
452452

453453
template <typename S, typename... Args,
454454
FMT_ENABLE_IF(is_compiled_string<S>::value)>
455-
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
456-
Args&&... args) {
455+
FMT_INLINE FMT_CONSTEXPR_STRING std::basic_string<typename S::char_type> format(
456+
const S&, Args&&... args) {
457457
if constexpr (std::is_same<typename S::char_type, char>::value) {
458458
constexpr auto str = basic_string_view<typename S::char_type>(S());
459459
if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') {

include/fmt/format.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,33 @@
117117
# define FMT_NOINLINE
118118
#endif
119119

120+
// Detect constexpr std::string.
121+
#if !FMT_USE_CONSTEVAL
122+
# define FMT_USE_CONSTEXPR_STRING 0
123+
#elif defined(__cpp_lib_constexpr_string) && \
124+
__cpp_lib_constexpr_string >= 201907L
125+
# if FMT_CLANG_VERSION && FMT_GLIBCXX_RELEASE
126+
// clang + libstdc++ are able to work only starting with gcc13.3
127+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294
128+
# if FMT_GLIBCXX_RELEASE < 13
129+
# define FMT_USE_CONSTEXPR_STRING 0
130+
# elif FMT_GLIBCXX_RELEASE == 13 && __GLIBCXX__ < 20240521
131+
# define FMT_USE_CONSTEXPR_STRING 0
132+
# else
133+
# define FMT_USE_CONSTEXPR_STRING 1
134+
# endif
135+
# else
136+
# define FMT_USE_CONSTEXPR_STRING 1
137+
# endif
138+
#else
139+
# define FMT_USE_CONSTEXPR_STRING 0
140+
#endif
141+
#if FMT_USE_CONSTEXPR_STRING
142+
# define FMT_CONSTEXPR_STRING constexpr
143+
#else
144+
# define FMT_CONSTEXPR_STRING
145+
#endif
146+
120147
// GCC 4.9 doesn't support qualified names in specializations.
121148
namespace std {
122149
template <typename T> struct iterator_traits<fmt::basic_appender<T>> {
@@ -4207,21 +4234,23 @@ FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args)
42074234
* std::string answer = fmt::to_string(42);
42084235
*/
42094236
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
4210-
FMT_NODISCARD auto to_string(T value) -> std::string {
4237+
FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(T value) -> std::string {
42114238
// The buffer should be large enough to store the number including the sign
42124239
// or "false" for bool.
42134240
char buffer[max_of(detail::digits10<T>() + 2, 5)];
42144241
return {buffer, detail::write<char>(buffer, value)};
42154242
}
42164243

42174244
template <typename T, FMT_ENABLE_IF(detail::use_format_as<T>::value)>
4218-
FMT_NODISCARD auto to_string(const T& value) -> std::string {
4245+
FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(const T& value)
4246+
-> std::string {
42194247
return to_string(format_as(value));
42204248
}
42214249

42224250
template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value &&
42234251
!detail::use_format_as<T>::value)>
4224-
FMT_NODISCARD auto to_string(const T& value) -> std::string {
4252+
FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(const T& value)
4253+
-> std::string {
42254254
auto buffer = memory_buffer();
42264255
detail::write<char>(appender(buffer), value);
42274256
return {buffer.data(), buffer.size()};

test/compile-test.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,49 @@ TEST(compile_time_formatting_test, multibyte_fill) {
421421
EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42));
422422
}
423423
#endif
424+
425+
#if FMT_USE_CONSTEXPR_STRING
426+
427+
TEST(compile_test, constexpr_format) {
428+
{
429+
constexpr auto result = []() {
430+
return fmt::format(FMT_COMPILE("{}"), 1) == "1";
431+
}();
432+
EXPECT_TRUE(result);
433+
}
434+
435+
{
436+
constexpr auto result = []() {
437+
return fmt::format(FMT_COMPILE("{:#b}"), 42) == "0b101010";
438+
}();
439+
EXPECT_TRUE(result);
440+
}
441+
442+
{
443+
constexpr auto result = []() {
444+
return "**-42" == fmt::format(FMT_COMPILE("{:*>5}"), -42);
445+
}();
446+
EXPECT_TRUE(result);
447+
}
448+
449+
{
450+
constexpr auto result = []() {
451+
return "10 " == fmt::format(FMT_COMPILE("{: ^3}"), 10);
452+
}();
453+
EXPECT_TRUE(result);
454+
}
455+
456+
{
457+
constexpr auto result = []() {
458+
return "42 is 42" == fmt::format(FMT_COMPILE("{} is {}"), 42, 42);
459+
}();
460+
EXPECT_TRUE(result);
461+
}
462+
463+
constexpr auto result = []() {
464+
return "This is a very huge int: 1234567890" == fmt::format(FMT_COMPILE("This is a very huge int: {}"), 1234567890);
465+
}();
466+
EXPECT_TRUE(result);
467+
}
468+
469+
#endif // FMT_USE_CONSTEXPR_STRING

0 commit comments

Comments
 (0)