Diagnose non-const-callable hash functions and comparators

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@291969 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2017-01-13 22:42:53 +00:00
parent ebaf7dab14
commit eaf292013d
6 changed files with 100 additions and 24 deletions

View File

@@ -24,7 +24,9 @@ struct BadCompare {
};
int main() {
static_assert(!std::__is_const_comparable<BadCompare, int>::value, "");
static_assert(!std::__invokable<BadCompare const&, int const&, int const&>::value, "");
static_assert(std::__invokable<BadCompare&, int const&, int const&>::value, "");
// expected-warning@__tree:* 4 {{the specified comparator type does not provide a const call operator}}
{
using C = std::set<int, BadCompare>;

View File

@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03
// REQUIRES: diagnose-if-support, verify-support
// Test that libc++ generates a warning diagnostic when the container is
// provided a non-const callable comparator.
#include <unordered_set>
#include <unordered_map>
struct BadHash {
template <class T>
size_t operator()(T const& t) {
return std::hash<T>{}(t);
}
};
struct BadEqual {
template <class T, class U>
bool operator()(T const& t, U const& u) {
return t == u;
}
};
int main() {
static_assert(!std::__invokable<BadEqual const&, int const&, int const&>::value, "");
static_assert(std::__invokable<BadEqual&, int const&, int const&>::value, "");
// expected-warning@__hash_table:* 4 {{the specified comparator type does not provide a const call operator}}
// expected-warning@__hash_table:* 4 {{the specified hash functor does not provide a const call operator}}
{
using C = std::unordered_set<int, BadHash, BadEqual>;
C s;
}
{
using C = std::unordered_multiset<long, BadHash, BadEqual>;
C s;
}
{
using C = std::unordered_map<int, int, BadHash, BadEqual>;
C s;
}
{
using C = std::unordered_multimap<long, int, BadHash, BadEqual>;
C s;
}
}