Skip to content

Commit ce6e7d8

Browse files
alexezedervitaut
authored andcommitted
use fixed_string to create named arg class with static name for _a literal
1 parent fc56af1 commit ce6e7d8

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

include/fmt/core.h

+14-13
Original file line numberDiff line numberDiff line change
@@ -979,16 +979,22 @@ struct arg_data<T, Char, NUM_ARGS, 0> {
979979
template <typename Char>
980980
inline void init_named_args(named_arg_info<Char>*, int, int) {}
981981

982-
template <typename Char, typename T, typename... Tail>
982+
template <typename T> struct is_named_arg : std::false_type {};
983+
984+
template <typename T, typename Char>
985+
struct is_named_arg<named_arg<Char, T>> : std::true_type {};
986+
987+
template <typename Char, typename T, typename... Tail,
988+
FMT_ENABLE_IF(!is_named_arg<T>::value)>
983989
void init_named_args(named_arg_info<Char>* named_args, int arg_count,
984990
int named_arg_count, const T&, const Tail&... args) {
985991
init_named_args(named_args, arg_count + 1, named_arg_count, args...);
986992
}
987993

988-
template <typename Char, typename T, typename... Tail>
994+
template <typename Char, typename T, typename... Tail,
995+
FMT_ENABLE_IF(is_named_arg<T>::value)>
989996
void init_named_args(named_arg_info<Char>* named_args, int arg_count,
990-
int named_arg_count, const named_arg<Char, T>& arg,
991-
const Tail&... args) {
997+
int named_arg_count, const T& arg, const Tail&... args) {
992998
named_args[named_arg_count++] = {arg.name, arg_count};
993999
init_named_args(named_args, arg_count + 1, named_arg_count, args...);
9941000
}
@@ -997,11 +1003,6 @@ template <typename... Args>
9971003
FMT_CONSTEXPR FMT_INLINE void init_named_args(std::nullptr_t, int, int,
9981004
const Args&...) {}
9991005

1000-
template <typename T> struct is_named_arg : std::false_type {};
1001-
1002-
template <typename T, typename Char>
1003-
struct is_named_arg<named_arg<Char, T>> : std::true_type {};
1004-
10051006
template <bool B = false> constexpr size_t count() { return B ? 1 : 0; }
10061007
template <bool B1, bool B2, bool... Tail> constexpr size_t count() {
10071008
return (B1 ? 1 : 0) + count<B2, Tail...>();
@@ -1277,10 +1278,10 @@ template <typename Context> struct arg_mapper {
12771278
return val;
12781279
}
12791280

1280-
template <typename T>
1281-
FMT_CONSTEXPR FMT_INLINE auto map(const named_arg<char_type, T>& val)
1282-
-> decltype(std::declval<arg_mapper>().map(val.value)) {
1283-
return map(val.value);
1281+
template <typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
1282+
FMT_CONSTEXPR FMT_INLINE auto map(const T& named_arg)
1283+
-> decltype(std::declval<arg_mapper>().map(named_arg.value)) {
1284+
return map(named_arg.value);
12841285
}
12851286

12861287
unformattable map(...) { return {}; }

include/fmt/format.h

+28
Original file line numberDiff line numberDiff line change
@@ -3941,13 +3941,32 @@ template <typename Char> struct udl_formatter {
39413941
}
39423942
};
39433943

3944+
# if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
3945+
template <typename T, typename Char, size_t N, fixed_string<Char, N> Str>
3946+
struct statically_named_arg : view {
3947+
static constexpr auto name = Str.data;
3948+
3949+
const T& value;
3950+
statically_named_arg(const T& v) : value(v) {}
3951+
};
3952+
3953+
template <typename T, typename Char, size_t N, fixed_string<Char, N> Str>
3954+
struct is_named_arg<statically_named_arg<T, Char, N, Str>> : std::true_type {};
3955+
3956+
template <typename Char, size_t N, fixed_string<Char, N> Str> struct udl_arg {
3957+
template <typename T> auto operator=(T&& value) const {
3958+
return statically_named_arg<T, Char, N, Str>(std::forward<T>(value));
3959+
}
3960+
};
3961+
# else
39443962
template <typename Char> struct udl_arg {
39453963
const Char* str;
39463964

39473965
template <typename T> named_arg<Char, T> operator=(T&& value) const {
39483966
return {str, std::forward<T>(value)};
39493967
}
39503968
};
3969+
# endif
39513970
} // namespace detail
39523971
FMT_MODULE_EXPORT_BEGIN
39533972

@@ -3981,12 +4000,21 @@ constexpr detail::udl_formatter<wchar_t> operator"" _format(const wchar_t* s,
39814000
fmt::print("Elapsed time: {s:.2f} seconds", "s"_a=1.23);
39824001
\endrst
39834002
*/
4003+
# if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
4004+
template <detail::fixed_string Str>
4005+
constexpr detail::udl_arg<remove_cvref_t<decltype(Str.data[0])>,
4006+
sizeof(Str.data) / sizeof(decltype(Str.data[0])), Str>
4007+
operator""_a() {
4008+
return {};
4009+
}
4010+
# else
39844011
constexpr detail::udl_arg<char> operator"" _a(const char* s, size_t) {
39854012
return {s};
39864013
}
39874014
constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
39884015
return {s};
39894016
}
4017+
# endif
39904018
} // namespace literals
39914019

39924020
FMT_MODULE_EXPORT_END

0 commit comments

Comments
 (0)