Skip to content

Export compiled_string so that user can customize one - #3999

Merged
vitaut merged 1 commit into
fmtlib:masterfrom
yujincheng08:patch-1
Jun 9, 2024
Merged

vitaut merged 1 commit into
fmtlib:masterfrom
yujincheng08:patch-1

Conversation

@yujincheng08

Copy link
Copy Markdown
Contributor

No description provided.

@vitaut

vitaut commented Jun 8, 2024

Copy link
Copy Markdown
Contributor

Thanks for the PR. Could you elaborate what you mean by customizing a compile string?

@yujincheng08

Copy link
Copy Markdown
Contributor Author

@vitaut

Hi, I would like to do something like the following:

template <size_t N> struct FixedString {
  constexpr inline FixedString(const char (&str)[N]) {
    std::copy_n(str, N, data);
  }
  constexpr inline FixedString() {}
  char data[N] = {};
};

template <FixedString Str>
struct CompiledString : fmt::detail::compiled_string {
  using char_type = char;
  explicit constexpr inline operator fmt::basic_string_view<char_type>() const {
    return {Str.data, sizeof(Str.data) - 1};
  }
};

template <FixedString Str, typename... T>
constexpr inline void LOG(T &&...args)
{
  std::array<char, 1024> buf{};
  auto s = fmt::format_to_n(buf.data(), buf.size(), CompiledString<Str>{},
                            std::forward<T>(args)...)
               .size;
  buf[s] = '\0';
  write_log(buf.data());
}


LOG<"hello {}">("world");

Since there's a FMT_ENABLE_IF(detail::is_compiled_string<S>::value) requirement from fmt::format_to_n, I have to derive fmt::detail::compiled_string to call the compiled version of format_to_n.

@vitaut

vitaut commented Jun 8, 2024

Copy link
Copy Markdown
Contributor

I think it would be better to move detail::udl_compiled_string to the public namespace (fmt) and drop the udl_ prefix. Then you won't need to reimplement it.

@yujincheng08

yujincheng08 commented Jun 8, 2024

Copy link
Copy Markdown
Contributor Author

@vitaut In fact, I aslo want to add std::source_location to my FixedString, and in this case the udl_compiled_string which only accepts fmt::exported_detail::fixed_string may not be enough (I need to use my own FixedString).

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case makes sense but let's move it to the public namespace (fmt) then.

@yujincheng08
yujincheng08 force-pushed the patch-1 branch 4 times, most recently from 64cf1fd to b562d03 Compare June 9, 2024 13:56
Comment thread include/fmt/compile.h Outdated
@vitaut
vitaut merged commit fad0222 into fmtlib:master Jun 9, 2024
@vitaut

vitaut commented Jun 9, 2024

Copy link
Copy Markdown
Contributor

Merged, thanks!

@yujincheng08
yujincheng08 deleted the patch-1 branch June 9, 2024 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants