Fix parse(container, function) for plain functions as callback
Passing a plain function or function pointer as parser callback, as in json::parse(std::string("[1,2]"), cb), stopped compiling in C++11/14/17. The iterator+sentinel overloads added for C++20 ranges are constrained by can_compare_ne, i.e. whether `first != last` is well-formed. The callback's parameter types make basic_json an associated class, so ADL finds the friend operator!=(const_reference, ScalarType): the container converts to basic_json and the function pointer is a scalar. The sentinel overload was thereby selected and failed hard in its body.
can_compare_ne now excludes function pointers and functions as sentinel type, since a callback is never a sentinel. This shared constraint guards parse, accept, sax_parse and all from_* overloads. Checking IteratorType via std::iterator_traits was avoided on purpose: it would instantiate iterator_traits for every two-argument call, which is a hard error on standard libraries with a non-SFINAE-friendly iterator_traits.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+65
-12
No files matched your search
Generated
+14
-6
@@ -10421,15 +10421,23 @@ struct can_compare_ne_either_order : std::integral_constant < bool,
|
||||
can_compare_ne_impl<IteratorType, SentinelType>::value ||
|
||||
can_compare_ne_reversed<IteratorType, SentinelType>::value > {};
|
||||
|
||||
// std::nullptr_t is excluded explicitly: a literal `nullptr` passed as a
|
||||
// trailing default argument (e.g. parse(s, nullptr, ...)) must never be
|
||||
// mistaken for a sentinel, and some compilers (e.g. GCC 4.8) unreliably
|
||||
// SFINAE the `operator!=` detection above for std::nullptr_t against
|
||||
// container/string types, which would otherwise make such calls ambiguous
|
||||
// with the compatible-input overload.
|
||||
// Two kinds of SentinelType are excluded explicitly:
|
||||
//
|
||||
// - std::nullptr_t: a literal `nullptr` passed as a trailing default argument
|
||||
// (e.g. parse(s, nullptr, ...)) must never be mistaken for a sentinel, and
|
||||
// some compilers (e.g. GCC 4.8) unreliably SFINAE the `operator!=` detection
|
||||
// above for std::nullptr_t against container/string types, which would
|
||||
// otherwise make such calls ambiguous with the compatible-input overload.
|
||||
// - function pointers and functions: a parser callback passed as plain function
|
||||
// (e.g. parse(s, cb)) is never a sentinel. The callback's parameter types
|
||||
// make basic_json an associated class, so ADL finds the friend
|
||||
// operator!=(const_reference, ScalarType) of basic_json, which makes
|
||||
// `s != cb` well-formed (both convert to basic_json) and would select the
|
||||
// iterator/sentinel overload.
|
||||
template<typename IteratorType, typename SentinelType>
|
||||
struct can_compare_ne : std::integral_constant < bool,
|
||||
!std::is_same<SentinelType, std::nullptr_t>::value &&
|
||||
!std::is_function<typename std::remove_pointer<SentinelType>::type>::value &&
|
||||
can_compare_ne_either_order<IteratorType, SentinelType>::value > {};
|
||||
|
||||
template<typename T>
|
||||
|
||||
Reference in new issue
Block a user