diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2016-10-21 02:36:37 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2016-10-21 02:36:37 +0000 |
commit | 0c1c53e3fad7fadb1c182b3e26e38378d2abe7f8 (patch) | |
tree | fb6ebc4ea1222b72b3768ce00b702fe8cc9df890 /clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp | |
parent | 14699cf1c601d2e8f0b6bf6b8e651a14a5a1e42d (diff) | |
download | bcm5719-llvm-0c1c53e3fad7fadb1c182b3e26e38378d2abe7f8.tar.gz bcm5719-llvm-0c1c53e3fad7fadb1c182b3e26e38378d2abe7f8.zip |
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.
llvm-svn: 284800
Diffstat (limited to 'clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp')
-rw-r--r-- | clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp b/clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp new file mode 100644 index 00000000000..53b6a3b2c42 --- /dev/null +++ b/clang/test/SemaCXX/libstdcxx_libcxx_less_hack.cpp @@ -0,0 +1,67 @@ +// 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}} +} |