Workaround Clang <4 tuple issue

Clang <4 fails to compile std::forward_as_tuple() calls involving
basic_json values. As a workaround, the values are wrapped using
std::reference_wrapper.
This commit is contained in:
Florian Albrechtskirchinger 2022-05-03 17:26:35 +02:00
parent f898c19aca
commit 6e1056dd53
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D
2 changed files with 76 additions and 6 deletions

View File

@ -5219,6 +5219,41 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_error<ConstThis>();
}
// workaround Clang <4 and GCC <5.2 not being able to construct tuples
// of basic_json by wrapping it using std::reference_wrapper
#if (defined(__clang__) && __clang_major__ < 4) || \
(defined(__GNUC__) && !JSON_HEDLEY_GCC_VERSION_CHECK(5, 2, 0))
using apply_basic_json_needs_to_be_wrapped = std::true_type;
#else
using apply_basic_json_needs_to_be_wrapped = std::false_type;
#endif
template<typename Arg>
using apply_arg_needs_to_be_wrapped = std::integral_constant < bool,
(apply_basic_json_needs_to_be_wrapped::value
&& detail::is_basic_json<detail::uncvref_t<Arg>>::value) >;
template < typename Arg,
detail::enable_if_t < apply_arg_needs_to_be_wrapped<Arg>::value&& std::is_const<Arg>::value, int > = 0 >
static auto apply_maybe_wrap_arg(Arg && arg) -> decltype(std::cref(std::forward<Arg>(arg)))
{
return std::cref(std::forward<Arg>(arg)); // LCOV_EXCL_LINE
}
template < typename Arg,
detail::enable_if_t < apply_arg_needs_to_be_wrapped<Arg>::value&& !std::is_const<Arg>::value, int > = 0 >
static auto apply_maybe_wrap_arg(Arg && arg) -> decltype(std::ref(std::forward<Arg>(arg)))
{
return std::ref(std::forward<Arg>(arg)); // LCOV_EXCL_LINE
}
template < typename Arg,
detail::enable_if_t < !apply_arg_needs_to_be_wrapped<Arg>::value, int > = 0 >
static auto apply_maybe_wrap_arg(Arg && arg) -> decltype(std::forward<Arg>(arg))
{
return std::forward<Arg>(arg);
}
// convert arguments to tuple; insert basic_json_value placeholder if missing
template < bool ConstThis, typename Value, typename ResultCallback, typename CallbackArg, typename Fn, typename FnArg, typename... Args,
detail::enable_if_t < std::is_member_pointer<Fn>::value
@ -5229,7 +5264,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_invoke<ConstThis>(
std::forward<Value>(val),
std::forward<ResultCallback>(cb), std::forward<CallbackArg>(cb_arg),
std::forward<Fn>(f), std::forward_as_tuple(f_arg, ::nlohmann::placeholders::basic_json_value, args...),
std::forward<Fn>(f), std::forward_as_tuple(f_arg, ::nlohmann::placeholders::basic_json_value, apply_maybe_wrap_arg(args)...),
detail::make_index_sequence < 2 + sizeof...(args) > ());
}
@ -5241,7 +5276,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_invoke<ConstThis>(
std::forward<Value>(val),
std::forward<ResultCallback>(cb), std::forward<CallbackArg>(cb_arg),
std::forward<Fn>(f), std::forward_as_tuple(::nlohmann::placeholders::basic_json_value, args...),
std::forward<Fn>(f), std::forward_as_tuple(::nlohmann::placeholders::basic_json_value, apply_maybe_wrap_arg(args)...),
detail::make_index_sequence < 1 + sizeof...(args) > ());
}
@ -5252,7 +5287,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_invoke<ConstThis>(
std::forward<Value>(val),
std::forward<ResultCallback>(cb), std::forward<CallbackArg>(cb_arg),
std::forward<Fn>(f), std::forward_as_tuple(args...),
std::forward<Fn>(f), std::forward_as_tuple(apply_maybe_wrap_arg(args)...),
detail::make_index_sequence<sizeof...(args)>());
}

View File

@ -23599,6 +23599,41 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_error<ConstThis>();
}
// workaround Clang <4 and GCC <5.2 not being able to construct tuples
// of basic_json by wrapping it using std::reference_wrapper
#if (defined(__clang__) && __clang_major__ < 4) || \
(defined(__GNUC__) && !JSON_HEDLEY_GCC_VERSION_CHECK(5, 2, 0))
using apply_basic_json_needs_to_be_wrapped = std::true_type;
#else
using apply_basic_json_needs_to_be_wrapped = std::false_type;
#endif
template<typename Arg>
using apply_arg_needs_to_be_wrapped = std::integral_constant < bool,
(apply_basic_json_needs_to_be_wrapped::value
&& detail::is_basic_json<detail::uncvref_t<Arg>>::value) >;
template < typename Arg,
detail::enable_if_t < apply_arg_needs_to_be_wrapped<Arg>::value&& std::is_const<Arg>::value, int > = 0 >
static auto apply_maybe_wrap_arg(Arg && arg) -> decltype(std::cref(std::forward<Arg>(arg)))
{
return std::cref(std::forward<Arg>(arg)); // LCOV_EXCL_LINE
}
template < typename Arg,
detail::enable_if_t < apply_arg_needs_to_be_wrapped<Arg>::value&& !std::is_const<Arg>::value, int > = 0 >
static auto apply_maybe_wrap_arg(Arg && arg) -> decltype(std::ref(std::forward<Arg>(arg)))
{
return std::ref(std::forward<Arg>(arg)); // LCOV_EXCL_LINE
}
template < typename Arg,
detail::enable_if_t < !apply_arg_needs_to_be_wrapped<Arg>::value, int > = 0 >
static auto apply_maybe_wrap_arg(Arg && arg) -> decltype(std::forward<Arg>(arg))
{
return std::forward<Arg>(arg);
}
// convert arguments to tuple; insert basic_json_value placeholder if missing
template < bool ConstThis, typename Value, typename ResultCallback, typename CallbackArg, typename Fn, typename FnArg, typename... Args,
detail::enable_if_t < std::is_member_pointer<Fn>::value
@ -23609,7 +23644,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_invoke<ConstThis>(
std::forward<Value>(val),
std::forward<ResultCallback>(cb), std::forward<CallbackArg>(cb_arg),
std::forward<Fn>(f), std::forward_as_tuple(f_arg, ::nlohmann::placeholders::basic_json_value, args...),
std::forward<Fn>(f), std::forward_as_tuple(f_arg, ::nlohmann::placeholders::basic_json_value, apply_maybe_wrap_arg(args)...),
detail::make_index_sequence < 2 + sizeof...(args) > ());
}
@ -23621,7 +23656,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_invoke<ConstThis>(
std::forward<Value>(val),
std::forward<ResultCallback>(cb), std::forward<CallbackArg>(cb_arg),
std::forward<Fn>(f), std::forward_as_tuple(::nlohmann::placeholders::basic_json_value, args...),
std::forward<Fn>(f), std::forward_as_tuple(::nlohmann::placeholders::basic_json_value, apply_maybe_wrap_arg(args)...),
detail::make_index_sequence < 1 + sizeof...(args) > ());
}
@ -23632,7 +23667,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
apply_invoke<ConstThis>(
std::forward<Value>(val),
std::forward<ResultCallback>(cb), std::forward<CallbackArg>(cb_arg),
std::forward<Fn>(f), std::forward_as_tuple(args...),
std::forward<Fn>(f), std::forward_as_tuple(apply_maybe_wrap_arg(args)...),
detail::make_index_sequence<sizeof...(args)>());
}