Skip to content

Commit

Permalink
Workaround for types with overloaded operator&
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <[email protected]>
  • Loading branch information
phprus committed Aug 5, 2023
1 parent b6e4c41 commit bdbbee2
Showing 1 changed file with 54 additions and 9 deletions.
63 changes: 54 additions & 9 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@
# define FMT_CONSTEXPR
#endif

#if FMT_MSC_VERSION && _MSC_FULL_VER >= 190024215
# define FMT_HAS_BUILTIN_ADDRESSOF
#elif FMT_GCC_VERSION >= 700
# define FMT_HAS_BUILTIN_ADDRESSOF
#elif defined(__has_builtin)
# if __has_builtin(__builtin_addressof)
# define FMT_HAS_BUILTIN_ADDRESSOF
# endif
#endif

#if FMT_CPLUSPLUS >= 201703L && defined(FMT_HAS_BUILTIN_ADDRESSOF)
# define FMT_CONSTEXPR_BUILTIN_ADDRESSOF constexpr
#else
# define FMT_CONSTEXPR_BUILTIN_ADDRESSOF
#endif

#if ((FMT_CPLUSPLUS >= 202002L) && \
(!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE > 9)) || \
(FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)
Expand Down Expand Up @@ -1233,6 +1249,23 @@ template <typename Context> struct custom_value {
void (*format)(void* arg, parse_context& parse_ctx, Context& ctx);
};

#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500
// A workaround for gcc 4.8 to make void_t work in a SFINAE context.
template <typename...> struct void_t_impl {
using type = void;
};
template <typename... T> using void_t = typename void_t_impl<T...>::type;
#else
template <typename...> using void_t = void;
#endif

template <typename T, typename Enable = void>
struct has_address_operator : std::false_type {};

template <typename T>
struct has_address_operator<T, void_t<decltype(std::declval<T&>().operator&())>>
: std::true_type {};

// A formatting argument value.
template <typename Context> class value {
public:
Expand Down Expand Up @@ -1281,7 +1314,26 @@ template <typename Context> class value {
FMT_INLINE value(const named_arg_info<char_type>* args, size_t size)
: named_args{args, size} {}

template <typename T> FMT_CONSTEXPR FMT_INLINE value(T& val) {
// Workaround for types with overloaded operator&.
// std::addressof is non-constexpr before C++17.
template <typename T, FMT_ENABLE_IF(has_address_operator<T>::value)>
FMT_CONSTEXPR_BUILTIN_ADDRESSOF FMT_INLINE value(T& val) {
using value_type = remove_const_t<T>;
#ifdef FMT_HAS_BUILTIN_ADDRESSOF
custom.value = const_cast<value_type*>(__builtin_addressof(val));
#else
custom.value = reinterpret_cast<value_type*>(
const_cast<char*>(&reinterpret_cast<const volatile char&>(val)));
#endif
// Get the formatter type through the context to allow different contexts
// have different extension points, e.g. `formatter<T>` for `format` and
// `printf_formatter<T>` for `printf`.
custom.format = format_custom_arg<
value_type, typename Context::template formatter_type<value_type>>;
}

template <typename T, FMT_ENABLE_IF(!has_address_operator<T>::value)>
FMT_CONSTEXPR FMT_INLINE value(T& val) {
using value_type = remove_const_t<T>;
custom.value = const_cast<value_type*>(&val);
// Get the formatter type through the context to allow different contexts
Expand All @@ -1290,6 +1342,7 @@ template <typename Context> class value {
custom.format = format_custom_arg<
value_type, typename Context::template formatter_type<value_type>>;
}

value(unformattable);
value(unformattable_char);
value(unformattable_pointer);
Expand Down Expand Up @@ -1502,14 +1555,6 @@ FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt {
return detail::copy_str<Char>(rng.begin(), rng.end(), out);
}

#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500
// A workaround for gcc 4.8 to make void_t work in a SFINAE context.
template <typename...> struct void_t_impl { using type = void; };
template <typename... T> using void_t = typename void_t_impl<T...>::type;
#else
template <typename...> using void_t = void;
#endif

template <typename It, typename T, typename Enable = void>
struct is_output_iterator : std::false_type {};

Expand Down

0 comments on commit bdbbee2

Please sign in to comment.