Merge pull request #2176 from gracicot/cpp20-support-no-std-fct-templ-specialization

C++20 support by removing swap specialization
This commit is contained in:
Niels Lohmann 2020-06-21 20:39:58 +02:00 committed by GitHub
commit 29ad2178c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 5 deletions

View file

@ -20,7 +20,11 @@
#endif #endif
// C++ language standard detection // C++ language standard detection
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#define JSON_HAS_CPP_20
#define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
#define JSON_HAS_CPP_17 #define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14 #define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)

View file

@ -5865,6 +5865,34 @@ class basic_json
/*! /*!
@brief exchanges the values @brief exchanges the values
Exchanges the contents of the JSON value from @a left with those of @a right. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated. implemented as a friend function callable via ADL.
@param[in,out] left JSON value to exchange the contents with
@param[in,out] right JSON value to exchange the contents with
@complexity Constant.
@liveexample{The example below shows how JSON values can be swapped with
`swap()`.,swap__reference}
@since version 1.0.0
*/
friend void swap(reference left, reference right) noexcept (
std::is_nothrow_move_constructible<value_t>::value and
std::is_nothrow_move_assignable<value_t>::value and
std::is_nothrow_move_constructible<json_value>::value and
std::is_nothrow_move_assignable<json_value>::value
)
{
left.swap(right);
}
/*!
@brief exchanges the values
Exchanges the contents of a JSON array with those of @a other. Does not Exchanges the contents of a JSON array with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is iterators and references remain valid. The past-the-end iterator is
@ -8657,6 +8685,9 @@ struct less<::nlohmann::detail::value_t>
} }
}; };
// C++20 prohibit function specialization in the std namespace.
#ifndef JSON_HAS_CPP_20
/*! /*!
@brief exchanges the values of two JSON objects @brief exchanges the values of two JSON objects
@ -8671,6 +8702,8 @@ inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcep
j1.swap(j2); j1.swap(j2);
} }
#endif
} // namespace std } // namespace std
/*! /*!

View file

@ -2048,7 +2048,11 @@ JSON_HEDLEY_DIAGNOSTIC_POP
#endif #endif
// C++ language standard detection // C++ language standard detection
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#define JSON_HAS_CPP_20
#define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
#define JSON_HAS_CPP_17 #define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14 #define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
@ -21655,6 +21659,34 @@ class basic_json
/*! /*!
@brief exchanges the values @brief exchanges the values
Exchanges the contents of the JSON value from @a left with those of @a right. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated. implemented as a friend function callable via ADL.
@param[in,out] left JSON value to exchange the contents with
@param[in,out] right JSON value to exchange the contents with
@complexity Constant.
@liveexample{The example below shows how JSON values can be swapped with
`swap()`.,swap__reference}
@since version 1.0.0
*/
friend void swap(reference left, reference right) noexcept (
std::is_nothrow_move_constructible<value_t>::value and
std::is_nothrow_move_assignable<value_t>::value and
std::is_nothrow_move_constructible<json_value>::value and
std::is_nothrow_move_assignable<json_value>::value
)
{
left.swap(right);
}
/*!
@brief exchanges the values
Exchanges the contents of a JSON array with those of @a other. Does not Exchanges the contents of a JSON array with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is iterators and references remain valid. The past-the-end iterator is
@ -24447,6 +24479,9 @@ struct less<::nlohmann::detail::value_t>
} }
}; };
// C++20 prohibit function specialization in the std namespace.
#ifndef JSON_HAS_CPP_20
/*! /*!
@brief exchanges the values of two JSON objects @brief exchanges the values of two JSON objects
@ -24461,6 +24496,8 @@ inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcep
j1.swap(j2); j1.swap(j2);
} }
#endif
} // namespace std } // namespace std
/*! /*!

View file

@ -154,7 +154,7 @@ TEST_CASE("concepts")
json j {1, 2, 3}; json j {1, 2, 3};
json::iterator it1 = j.begin(); json::iterator it1 = j.begin();
json::iterator it2 = j.end(); json::iterator it2 = j.end();
std::swap(it1, it2); swap(it1, it2);
CHECK(it1 == j.end()); CHECK(it1 == j.end());
CHECK(it2 == j.begin()); CHECK(it2 == j.begin());
} }
@ -162,7 +162,7 @@ TEST_CASE("concepts")
json j {1, 2, 3}; json j {1, 2, 3};
json::const_iterator it1 = j.cbegin(); json::const_iterator it1 = j.cbegin();
json::const_iterator it2 = j.cend(); json::const_iterator it2 = j.cend();
std::swap(it1, it2); swap(it1, it2);
CHECK(it1 == j.end()); CHECK(it1 == j.end());
CHECK(it2 == j.begin()); CHECK(it2 == j.begin());
} }

View file

@ -878,7 +878,8 @@ TEST_CASE("modifiers")
json j("hello world"); json j("hello world");
json k(42.23); json k(42.23);
std::swap(j, k); using std::swap;
swap(j, k);
CHECK(j == json(42.23)); CHECK(j == json(42.23));
CHECK(k == json("hello world")); CHECK(k == json("hello world"));