-
Notifications
You must be signed in to change notification settings - Fork 2.6k
reimplement formatter<tuple_join_view>
#2457
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I have no idea the best appropriate function name. It may need a change.
I suggest renaming parse_subroutine
and format_subroutine
to do_parse
and do_format
respectively which is a common pattern for helper functions like this.
Also the
index_helper
can be replaced bystd::integral_constant
.
Please do, replace index_helper
with std::integral_constant
.
1. completely reimplement `formatter<tuple_join_view<Char, T...>, Char>` 2. Add some tests
Thank you! |
Very cool. |
One potential optimization is to create one formatter per type instead of one formatter per tuple element. Then |
This can be done with a lot of extra work, though. We basically need to
I think this is possible with these technologies demonstrated below. https://godbolt.org/z/h41q9Wav5 // check whether T is in Ts...
template <typename T, typename... Ts>
struct appear : public std::integral_constant<bool, false> {};
template <typename T, typename T1, typename... Ts>
struct appear<T, T1, Ts...>
: public std::integral_constant<
bool, std::is_same<T, T1>::value ? true : appear<T, Ts...>::value> {};
// we need this helper function to unpack std::tuple
template <typename T, typename... Ts>
typename std::conditional<appear<T, Ts...>::value, std::tuple<Ts...>,
std::tuple<T, Ts...>>::type
get_type(std::tuple<Ts...>);
// deduplicate to get std::tuple<...>
// for example:
// deduplicate<int, int, double, int>::type is std::tuple<double, int>
template <typename... Ts>
struct deduplicate {
using type = std::tuple<>;
};
template <typename T, typename... Ts>
struct deduplicate<T, Ts...> {
using type = decltype(get_type<T>(std::declval<typename deduplicate<Ts...>::type>()));
}; and for the second part // get the index of a type from the tuple
template <typename T, typename... Ts>
struct get_index : public std::integral_constant<size_t, 0> {};
template <typename T, typename T1, typename... Ts>
struct get_index<T, T1, Ts...>
: public std::integral_constant<
size_t,
std::is_same<T, T1>::value ? 0 : get_index<T, Ts...>::value + 1> {};
template <typename T, typename T1>
struct get_tuple_index;
template <typename T, typename... Ts>
struct get_tuple_index<T, std::tuple<Ts...>> {
size_t value = get_index<T, Ts...>::value;
}; As for this, we also need to handle |
* reimplement `formatter<tuple_join_view>` 1. completely reimplement `formatter<tuple_join_view<Char, T...>, Char>` 2. Add some tests * use FMT_THROW
PR for #2451. I reimplemented that formatter.
It supports the following code: (Tested on clang12 and gcc11, for std=c++11/17/20)
I add two functions
parse_subroutine
andformat_subroutine
as subroutines ofparse
andformat
, respectively.I have no idea the best appropriate function name. It may need a change. Also the
index_helper
can be replaced bystd::integral_constant
.This code is only a prototype to demostrate my ideas, and not in its best shape. Comments are welcome.