大家一般都是先关心参数,然后最后再看返回的是什么类型。
在这里把返回类型后置,可能就是一种逻辑上的体现吧
fmt的一个函数。
\fmt\core.h
这个函数的意义,应该就是用变长参数初始化成一个format_arg_store类型的变量,并返回。
\fmt\bundled\core.h
/**
\rst
Constructs a `~fmt::format_arg_store` object that contains references to
arguments and can be implicitly converted to `~fmt::format_args`. `Context`
can be omitted in which case it defaults to `~fmt::context`.
See `~fmt::arg` for lifetime considerations.
\endrst
*/
template <typename Context = format_context, typename... Args>
constexpr auto make_format_args(const Args&... args)
-> format_arg_store<Context, Args...> {
return {args...};
}
但这个怎么回事,传进double,返回的还是double:
FMT_CONSTEXPR FMT_INLINE auto map(double val) -> double { return val; }
可能是因为重载其他map模板函数,比如:
template <typename T, FMT_ENABLE_IF(is_char<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(T val) -> char_type {
static_assert(
std::is_same<T, char>::value || std::is_same<T, char_type>::value,
"mixing character types is disallowed");
return val;
}
注意,这个char_type是通过模板参数而来的:
using char_type = typename Context::char_type;
\fmt\bundled\core.h
// Maps formatting arguments to core types.
template <typename Context> struct arg_mapper
{
using char_type = typename Context::char_type;
FMT_CONSTEXPR FMT_INLINE auto map(signed char val) -> int { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(unsigned char val) -> unsigned {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(short val) -> int { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(unsigned short val) -> unsigned {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(int val) -> int { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(unsigned val) -> unsigned { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(long val) -> long_type { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(unsigned long val) -> ulong_type {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(long long val) -> long long { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(unsigned long long val)
-> unsigned long long {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(int128_t val) -> int128_t { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(uint128_t val) -> uint128_t { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(bool val) -> bool { return val; }
template <typename T, FMT_ENABLE_IF(is_char<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(T val) -> char_type {
static_assert(
std::is_same<T, char>::value || std::is_same<T, char_type>::value,
"mixing character types is disallowed");
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(float val) -> float { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(double val) -> double { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(long double val) -> long double {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(char_type* val) -> const char_type* {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(const char_type* val) -> const char_type* {
return val;
}
template <typename T, FMT_ENABLE_IF(is_string<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val)
-> basic_string_view<char_type> {
static_assert(std::is_same<char_type, char_t<T>>::value,
"mixing character types is disallowed");
return to_string_view(val);
}
template <typename T,
FMT_ENABLE_IF(
std::is_constructible<basic_string_view<char_type>, T>::value &&
!is_string<T>::value && !has_formatter<T, Context>::value &&
!has_fallback_formatter<T, char_type>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val)
-> basic_string_view<char_type> {
return basic_string_view<char_type>(val);
}
template <
typename T,
FMT_ENABLE_IF(
std::is_constructible<std_string_view<char_type>, T>::value &&
!std::is_constructible<basic_string_view<char_type>, T>::value &&
!is_string<T>::value && !has_formatter<T, Context>::value &&
!has_fallback_formatter<T, char_type>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val)
-> basic_string_view<char_type> {
return std_string_view<char_type>(val);
}
FMT_CONSTEXPR FMT_INLINE auto map(const signed char* val) -> const char* {
static_assert(std::is_same<char_type, char>::value, "invalid string type");
return reinterpret_cast<const char*>(val);
}
FMT_CONSTEXPR FMT_INLINE auto map(const unsigned char* val) -> const char* {
static_assert(std::is_same<char_type, char>::value, "invalid string type");
return reinterpret_cast<const char*>(val);
}
FMT_CONSTEXPR FMT_INLINE auto map(signed char* val) -> const char* {
const auto* const_val = val;
return map(const_val);
}
FMT_CONSTEXPR FMT_INLINE auto map(unsigned char* val) -> const char* {
const auto* const_val = val;
return map(const_val);
}
FMT_CONSTEXPR FMT_INLINE auto map(void* val) -> const void* { return val; }
FMT_CONSTEXPR FMT_INLINE auto map(const void* val) -> const void* {
return val;
}
FMT_CONSTEXPR FMT_INLINE auto map(std::nullptr_t val) -> const void* {
return val;
}
// We use SFINAE instead of a const T* parameter to avoid conflicting with
// the C array overload.
template <typename T>
FMT_CONSTEXPR auto map(T) -> enable_if_t<std::is_pointer<T>::value, int> {
// Formatting of arbitrary pointers is disallowed. If you want to output
// a pointer cast it to "void *" or "const void *". In particular, this
// forbids formatting of "[const] volatile char *" which is printed as bool
// by iostreams.
static_assert(!sizeof(T), "formatting of non-void pointers is disallowed");
return 0;
}
template <typename T, std::size_t N>
FMT_CONSTEXPR FMT_INLINE auto map(const T (&values)[N]) -> const T (&)[N] {
return values;
}
template <typename T,
FMT_ENABLE_IF(std::is_enum<T>::value &&
!has_formatter<T, Context>::value &&
!has_fallback_formatter<T, char_type>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val)
-> decltype(std::declval<arg_mapper>().map(
static_cast<typename std::underlying_type<T>::type>(val))) {
return map(static_cast<typename std::underlying_type<T>::type>(val));
}
template <typename T,
FMT_ENABLE_IF(!is_string<T>::value && !is_char<T>::value &&
(has_formatter<T, Context>::value ||
has_fallback_formatter<T, char_type>::value))>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> const T& {
return val;
}
template <typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& named_arg)
-> decltype(std::declval<arg_mapper>().map(named_arg.value)) {
return map(named_arg.value);
}
auto map(...) -> unformattable { return {}; }
}