llvm-capstone/clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp
Richard Smith 5e9746f520 DR583, DR1512: Implement a rewrite to C++'s 'composite pointer type' rules.
This has two significant effects:

1) Direct relational comparisons between null pointer constants (0 and nullopt)
   and pointers are now ill-formed. This was always the case for C, and it
   appears that C++ only ever permitted by accident. For instance, cases like
     nullptr < &a
   are now rejected.

2) Comparisons and conditional operators between differently-cv-qualified
   pointer types now work, and produce a composite type that both source
   pointer types can convert to (when possible). For instance, comparison
   between 'int **' and 'const int **' is now valid, and uses an intermediate
   type of 'const int *const *'.

Clang previously supported #2 as an extension.

We do not accept the cases in #1 as an extension. I've tested a fair amount of
code to check that this doesn't break it, but if it turns out that someone is
relying on this, we can easily add it back as an extension.

This is a re-commit of r284800.

llvm-svn: 284890
2016-10-21 22:00:42 +00:00

68 lines
1.9 KiB
C++

// This is a test for a hack in Clang that works around a problem introduced by
// DR583: it's no longer possible to compare a pointer against nullptr_t, but
// we still want to permit those comparisons within less<> and friends.
// RUN: %clang_cc1 -verify %s -std=c++14
namespace std {
template<typename T = void> struct less {};
template<typename T = void> struct less_equal {};
template<typename T = void> struct greater {};
template<typename T = void> struct greater_equal {};
template<> struct less<> {
template <class T1, class T2>
auto operator()(T1 &&t, T2 &&u) const noexcept(noexcept(t < u))
-> decltype(t < u) {
return t < u;
}
};
template<> struct less_equal<> {
template <class T1, class T2>
auto operator()(T1 &&t, T2 &&u) const noexcept(noexcept(t <= u))
-> decltype(t <= u) {
return t <= u;
}
};
template<> struct greater<> {
template <class T1, class T2>
auto operator()(T1 &&t, T2 &&u) const noexcept(noexcept(t > u))
-> decltype(t > u) {
return t > u;
}
};
template<> struct greater_equal<> {
template <class T1, class T2>
auto operator()(T1 &&t, T2 &&u) const noexcept(noexcept(t >= u))
-> decltype(t >= u) {
return t >= u;
}
};
template<typename = void> struct unrelated;
template<> struct unrelated<> {
template <class T1, class T2>
auto operator()(T1 &&t, T2 &&u) const noexcept(noexcept(t < u)) // expected-note {{substitution failure}}
-> decltype(t < u) {
return t < u;
}
};
};
void test(int *p) {
using namespace std;
less<>()(p, nullptr);
less<>()(nullptr, p);
less_equal<>()(p, nullptr);
less_equal<>()(nullptr, p);
greater<>()(p, nullptr);
greater<>()(nullptr, p);
greater_equal<>()(p, nullptr);
greater_equal<>()(nullptr, p);
unrelated<>()(p, nullptr); // expected-error {{no matching function}}
}