202 lines · cpp
1// RUN: %clang_cc1 -std=c++2a -verify -triple x86_64-linux-gnu %s2 3template<typename T> concept C1 = true; // expected-note{{template is declared here}}4static_assert(C1<int>);5static_assert(C1);6// expected-error@-1{{use of concept 'C1' requires template arguments}}7 8template<typename T> concept C2 = sizeof(T) == 4;9static_assert(C2<int>);10static_assert(!C2<long long int>);11static_assert(C2<char[4]>);12static_assert(!C2<char[5]>);13 14template<typename T> concept C3 = sizeof(*T{}) == 4;15static_assert(C3<int*>);16static_assert(!C3<long long int>);17 18struct A {19 static constexpr int add(int a, int b) {20 return a + b;21 }22};23struct B {24 static int add(int a, int b) { // expected-note{{declared here}}25 return a + b;26 }27};28template<typename U>29concept C4 = U::add(1, 2) == 3;30// expected-error@-1{{substitution into constraint expression resulted in a non-constant expression}}31// expected-note@-2{{non-constexpr function 'add' cannot be used in a constant expression}}32static_assert(C4<A>);33static_assert(!C4<B>); // expected-note {{while checking the satisfaction of concept 'C4<B>' requested here}}34 35template<typename T, typename U>36constexpr bool is_same_v = false;37 38template<typename T>39constexpr bool is_same_v<T, T> = true;40 41template<typename T, typename U>42concept Same = is_same_v<T, U>;43 44static_assert(Same<int, int>);45static_assert(Same<int, decltype(1)>);46static_assert(!Same<int, unsigned int>);47static_assert(!Same<A, B>);48static_assert(Same<A, A>);49 50static_assert(Same<bool, decltype(C1<int>)>);51static_assert(Same<bool, decltype(C2<int>)>);52static_assert(Same<bool, decltype(C3<int*>)>);53static_assert(Same<bool, decltype(C4<A>)>);54 55template<typename T> concept C5 = T{}; // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}56constexpr bool x = C5<int>; // expected-note {{while checking the satisfaction of concept 'C5<int>' requested here}}57 58template<int x>59concept IsEven = (x % 2) == 0;60 61static_assert(IsEven<20>);62static_assert(!IsEven<11>);63 64template<template<typename T> typename P>65concept IsTypePredicate = is_same_v<decltype(P<bool>::value), const bool>66 && is_same_v<decltype(P<int>::value), const bool>67 && is_same_v<decltype(P<long long>::value), const bool>;68 69template<typename T> struct T1 {};70template<typename T> struct T2 { static constexpr bool value = sizeof(T) == 2; };71 72static_assert(IsTypePredicate<T2>);73static_assert(!IsTypePredicate<T1>);74 75template<typename T, typename U, typename... Ts>76concept OneOf = (Same<T, Ts> || ...);77 78static_assert(OneOf<int, long, int>);79static_assert(!OneOf<long, int, char, char>);80 81namespace piecewise_substitution {82 template <typename T>83 concept True = true;84 85 template <typename T>86 concept A = True<T> || T::value;87 88 template <typename T>89 concept B = (True<T> || T::value);90 91 template <typename T>92 concept C = !True<T> && T::value || true;93 94 template <typename T>95 concept D = (!True<T> && T::value) || true;96 97 template <typename T>98 concept E = T::value || True<T>;99 100 template <typename T>101 concept F = (T::value || True<T>);102 103 template <typename T>104 concept G = T::value && !True<T> || true;105 106 template <typename T>107 concept H = (T::value && !True<T>) || true;108 109 template <typename T>110 concept I = T::value;111 112 static_assert(A<int>);113 static_assert(B<int>);114 static_assert(C<int>);115 static_assert(D<int>);116 static_assert(E<int>);117 static_assert(F<int>);118 static_assert(G<int>);119 static_assert(H<int>);120 static_assert(!I<int>);121}122 123// Short ciruiting124 125template<typename T> struct T3 { using type = typename T::type; };126// expected-error@-1{{type 'char' cannot be used prior to '::' because it has no members}}127// expected-error@-2{{type 'short' cannot be used prior to '::' because it has no members}}128 129template<typename T>130concept C6 = sizeof(T) == 1 && sizeof(typename T3<T>::type) == 1;131// expected-note@-1{{while substituting template arguments into constraint expression here}}132// expected-note@-2{{in instantiation of template class 'T3<char>' requested here}}133 134template<typename T>135concept C7 = sizeof(T) == 1 || sizeof(136// expected-note@-1{{while substituting template arguments into constraint expression here}}137 typename138 T3<T>139// expected-note@-1{{in instantiation of template class 'T3<short>' requested here}}140 ::type) == 1;141 142static_assert(!C6<short>);143static_assert(!C6<char>);144// expected-note@-1 {{while checking the satisfaction of concept 'C6<char>' requested here}}145static_assert(C7<char>);146static_assert(!C7<short>); // expected-note{{while checking the satisfaction of concept 'C7<short>' requested here}}147 148// Make sure argument list is converted when instantiating a CSE.149 150template<typename T, typename U = int>151concept SameSize = sizeof(T) == sizeof(U);152 153template<typename T>154struct X { static constexpr bool a = SameSize<T>; };155 156static_assert(X<unsigned>::a);157 158// static_assert concept diagnostics159template<typename T>160concept Large = sizeof(T) > 100;161// expected-note@-1 2{{because 'sizeof(small) > 100' (1 > 100) evaluated to false}}162 163struct small { };164static_assert(Large<small>);165// expected-error@-1 {{static assertion failed}}166// expected-note@-2 {{because 'small' does not satisfy 'Large'}}167static_assert(Large<small>, "small isn't large");168// expected-error@-1 {{static assertion failed: small isn't large}}169// expected-note@-2 {{because 'small' does not satisfy 'Large'}}170 171// Make sure access-checking can fail a concept specialization172 173class T4 { static constexpr bool f = true; };174template<typename T> concept AccessPrivate = T{}.f;175// expected-note@-1{{because substituted constraint expression is ill-formed: 'f' is a private member of 'T4'}}176static_assert(AccessPrivate<T4>);177// expected-error@-1{{static assertion failed}}178// expected-note@-2{{because 'T4' does not satisfy 'AccessPrivate'}}179 180template<typename T, typename U>181// expected-note@-1{{template parameter is declared here}}182concept C8 = sizeof(T) > sizeof(U);183 184template<typename... T>185constexpr bool B8 = C8<T...>;186// expected-error@-1{{pack expansion used as argument for non-pack parameter of concept}}187 188 189// Make sure we correctly check for containsUnexpandedParameterPack190 191template<typename T>192concept C9 = true;193 194template <typename Fn, typename... Args>195using invoke = typename Fn::template invoke<Args...>;196 197template <typename C, typename... L>198// The converted argument here will not containsUnexpandedParameterPack, but the199// as-written one will.200requires (C9<invoke<C, L>> &&...)201struct S { };202