Skip to content

Commit

Permalink
✨ added overload for std::less<value_t> #486
Browse files Browse the repository at this point in the history
MSVC needs this overload to compile code containing a std::map that
uses nlohmann::detail::operator as key.
  • Loading branch information
nlohmann committed Mar 11, 2017
1 parent 758c4ad commit f4126e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/json.hpp
Expand Up @@ -13025,6 +13025,22 @@ struct hash<nlohmann::json>
return h(j.dump());
}
};

/// specialization for std::less<value_t>
template <>
struct less<::nlohmann::detail::value_t>
{
/*!
@brief compare two value_t enum values
@since version 3.0.0
*/
bool operator()(nlohmann::detail::value_t lhs,
nlohmann::detail::value_t rhs) const noexcept
{
return nlohmann::detail::operator<(lhs, rhs);
}
};

} // namespace std

/*!
Expand Down
16 changes: 16 additions & 0 deletions src/json.hpp.re2c
Expand Up @@ -12059,6 +12059,22 @@ struct hash<nlohmann::json>
return h(j.dump());
}
};

/// specialization for std::less<value_t>
template <>
struct less<::nlohmann::detail::value_t>
{
/*!
@brief compare two value_t enum values
@since version 3.0.0
*/
bool operator()(nlohmann::detail::value_t lhs,
nlohmann::detail::value_t rhs) const noexcept
{
return nlohmann::detail::operator<(lhs, rhs);
}
};

} // namespace std

/*!
Expand Down
7 changes: 7 additions & 0 deletions test/src/unit-regression.cpp
Expand Up @@ -795,4 +795,11 @@ TEST_CASE("regression tests")
std::string s2 = j2.dump();
CHECK(s1 == s2);
}

SECTION("issue #486 - json::value_t can't be a map's key type in VC++ 2015")
{
// the code below must compile with MSVC
std::map<json::value_t, std::string> jsonTypes ;
jsonTypes[json::value_t::array] = "array";
}
}

0 comments on commit f4126e4

Please sign in to comment.