Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export compiled_string so that user can customize one #3999

Merged
merged 1 commit into from
Jun 9, 2024

Conversation

yujincheng08
Copy link
Contributor

No description provided.

@vitaut
Copy link
Contributor

vitaut commented Jun 8, 2024

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

@yujincheng08
Copy link
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
Copy link
Contributor

vitaut commented Jun 8, 2024

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
Copy link
Contributor Author

yujincheng08 commented Jun 8, 2024

@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).

Copy link
Contributor

@vitaut vitaut left a comment

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
include/fmt/compile.h Outdated Show resolved Hide resolved
@vitaut vitaut merged commit fad0222 into fmtlib:master Jun 9, 2024
42 checks passed
@vitaut
Copy link
Contributor

vitaut commented Jun 9, 2024

Merged, thanks!

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.

None yet

2 participants