diff options
| author | Saar Raz <saar@raz.email> | 2019-12-06 01:30:21 +0200 |
|---|---|---|
| committer | Saar Raz <saar@raz.email> | 2019-12-06 01:34:20 +0200 |
| commit | fdf80e86a52849813d05da4b6c25884c06ba9e98 (patch) | |
| tree | f08ca57be85567d41a118bedb588a8152763c360 /clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp | |
| parent | 7faa8440440f280912c33a27c14c0dc4031532de (diff) | |
| download | bcm5719-llvm-fdf80e86a52849813d05da4b6c25884c06ba9e98.tar.gz bcm5719-llvm-fdf80e86a52849813d05da4b6c25884c06ba9e98.zip | |
[Concepts] Constraint Enforcement & Diagnostics
Part of the C++20 concepts implementation effort.
- Associated constraints (requires clauses, currently) are now enforced when instantiating/specializing templates and when considering partial specializations and function overloads.
- Elaborated diagnostics give helpful insight as to why the constraints were not satisfied.
Phabricator: D41569
Re-commit, after fixing some memory bugs.
Diffstat (limited to 'clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp')
| -rw-r--r-- | clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp index dd3f0c0e3d6..1e10d4550ce 100644 --- a/clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.id/p3.cpp @@ -72,6 +72,15 @@ template<typename T> struct T2 { static constexpr bool value = sizeof(T) == 2; } static_assert(IsTypePredicate<T2>); static_assert(!IsTypePredicate<T1>); +template<typename T, typename U, typename... Ts> +concept OneOf = (Same<T, Ts> || ...); + +template<typename... X> +constexpr bool S = OneOf<X..., int, int>; + +static_assert(S<int, long, int>); +static_assert(!S<long, int, char, char>); + namespace piecewise_substitution { template <typename T> concept True = true; @@ -147,3 +156,25 @@ template<typename T> struct X { static constexpr bool a = SameSize<T>; }; static_assert(X<unsigned>::a); + +// static_assert concept diagnostics +template<typename T> +concept Large = sizeof(T) > 100; +// expected-note@-1 2{{because 'sizeof(small) > 100' (1 > 100) evaluated to false}} + +struct small { }; +static_assert(Large<small>); +// expected-error@-1 {{static_assert failed}} +// expected-note@-2 {{because 'small' does not satisfy 'Large'}} +static_assert(Large<small>, "small isn't large"); +// expected-error@-1 {{static_assert failed "small isn't large"}} +// expected-note@-2 {{because 'small' does not satisfy 'Large'}} + +// Make sure access-checking can fail a concept specialization + +class T4 { static constexpr bool f = true; }; +template<typename T> concept AccessPrivate = T{}.f; +// expected-note@-1{{because substituted constraint expression is ill-formed: 'f' is a private member of 'T4'}} +static_assert(AccessPrivate<T4>); +// expected-error@-1{{static_assert failed}} +// expected-note@-2{{because 'T4' does not satisfy 'AccessPrivate'}} |

