Merge pull request #1294 from theodelrieu/fix/json_ref_ctor

add constraints for variadic json_ref constructors
This commit is contained in:
Niels Lohmann 2018-10-12 17:35:41 +02:00 committed by GitHub
commit dd672939a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 8 deletions

View file

@ -3,6 +3,8 @@
#include <initializer_list> #include <initializer_list>
#include <utility> #include <utility>
#include <nlohmann/detail/meta/type_traits.hpp>
namespace nlohmann namespace nlohmann
{ {
namespace detail namespace detail
@ -25,10 +27,12 @@ class json_ref
: owned_value(init), value_ref(&owned_value), is_rvalue(true) : owned_value(init), value_ref(&owned_value), is_rvalue(true)
{} {}
template<class... Args> template <
json_ref(Args&& ... args) class... Args,
: owned_value(std::forward<Args>(args)...), value_ref(&owned_value), is_rvalue(true) enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
{} json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
is_rvalue(true) {}
// class should be movable only // class should be movable only
json_ref(json_ref&&) = default; json_ref(json_ref&&) = default;

View file

@ -10591,6 +10591,9 @@ class serializer
#include <initializer_list> #include <initializer_list>
#include <utility> #include <utility>
// #include <nlohmann/detail/meta/type_traits.hpp>
namespace nlohmann namespace nlohmann
{ {
namespace detail namespace detail
@ -10613,10 +10616,12 @@ class json_ref
: owned_value(init), value_ref(&owned_value), is_rvalue(true) : owned_value(init), value_ref(&owned_value), is_rvalue(true)
{} {}
template<class... Args> template <
json_ref(Args&& ... args) class... Args,
: owned_value(std::forward<Args>(args)...), value_ref(&owned_value), is_rvalue(true) enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
{} json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
is_rvalue(true) {}
// class should be movable only // class should be movable only
json_ref(json_ref&&) = default; json_ref(json_ref&&) = default;

View file

@ -33,6 +33,14 @@ SOFTWARE.
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
#define JSON_HAS_CPP_17
#endif
#ifdef JSON_HAS_CPP_17
#include <variant>
#endif
#include "fifo_map.hpp" #include "fifo_map.hpp"
#include <fstream> #include <fstream>
@ -1649,4 +1657,12 @@ TEST_CASE("regression tests")
CHECK(diffs.size() == 1); // Note the change here, was 2 CHECK(diffs.size() == 1); // Note the change here, was 2
} }
#ifdef JSON_HAS_CPP_17
SECTION("issue #1292 - Serializing std::variant causes stack overflow")
{
static_assert(
not std::is_constructible<json, std::variant<int, float>>::value, "");
}
#endif
} }