Simplify is_ordered_map to reuse has_capacity
is_ordered_map re-detected capacity() with a C++03 sizeof/vararg trick right after has_capacity did the same detection through is_detected. For ordered_map, the old trick took the address of std::vector::capacity, which [namespace.std]/6 makes unspecified. Reuse has_capacity instead, which removes the unspecified-behavior pointer-to-std-member and two NOLINT suppressions. Part of #5708 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+14
-28
No files matched your search
@@ -805,20 +805,7 @@ struct has_capacity : std::integral_constant<bool, is_detected<detect_capacity,
|
|||||||
// a naive helper to check if a type is an ordered_map (exploits the fact that
|
// a naive helper to check if a type is an ordered_map (exploits the fact that
|
||||||
// ordered_map inherits capacity() from std::vector)
|
// ordered_map inherits capacity() from std::vector)
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct is_ordered_map
|
struct is_ordered_map : has_capacity<T> {};
|
||||||
{
|
|
||||||
using one = char;
|
|
||||||
|
|
||||||
struct two
|
|
||||||
{
|
|
||||||
char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename C> static one test( decltype(&C::capacity) ) ;
|
|
||||||
template <typename C> static two test(...);
|
|
||||||
|
|
||||||
enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,cppcoreguidelines-use-enum-class)
|
|
||||||
};
|
|
||||||
|
|
||||||
// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
|
// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
|
||||||
template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
|
template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
|
||||||
|
|||||||
@@ -4785,20 +4785,7 @@ struct has_capacity : std::integral_constant<bool, is_detected<detect_capacity,
|
|||||||
// a naive helper to check if a type is an ordered_map (exploits the fact that
|
// a naive helper to check if a type is an ordered_map (exploits the fact that
|
||||||
// ordered_map inherits capacity() from std::vector)
|
// ordered_map inherits capacity() from std::vector)
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct is_ordered_map
|
struct is_ordered_map : has_capacity<T> {};
|
||||||
{
|
|
||||||
using one = char;
|
|
||||||
|
|
||||||
struct two
|
|
||||||
{
|
|
||||||
char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename C> static one test( decltype(&C::capacity) ) ;
|
|
||||||
template <typename C> static two test(...);
|
|
||||||
|
|
||||||
enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,cppcoreguidelines-use-enum-class)
|
|
||||||
};
|
|
||||||
|
|
||||||
// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
|
// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
|
||||||
template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
|
template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
|
||||||
|
|||||||
@@ -10,10 +10,14 @@
|
|||||||
|
|
||||||
#if JSON_TEST_USING_MULTIPLE_HEADERS
|
#if JSON_TEST_USING_MULTIPLE_HEADERS
|
||||||
#include <nlohmann/detail/meta/type_traits.hpp>
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
||||||
|
#include <nlohmann/ordered_map.hpp>
|
||||||
#else
|
#else
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
TEST_CASE("type traits")
|
TEST_CASE("type traits")
|
||||||
{
|
{
|
||||||
SECTION("is_c_string")
|
SECTION("is_c_string")
|
||||||
@@ -83,4 +87,12 @@ TEST_CASE("type traits")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("is_ordered_map")
|
||||||
|
{
|
||||||
|
using nlohmann::detail::is_ordered_map;
|
||||||
|
|
||||||
|
CHECK(is_ordered_map<nlohmann::ordered_map<std::string, int>>::value);
|
||||||
|
CHECK_FALSE(is_ordered_map<std::map<std::string, int>>::value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user