1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -triple=x86_64-linux-gnu 2 3 int f(); // expected-note {{declared here}} 4 5 static_assert(f(), "f"); // expected-error {{static_assert expression is not an integral constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}} 6 static_assert(true, "true is not false"); 7 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}} 8 9 void g() { 10 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}} 11 } 12 13 class C { 14 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}} 15 }; 16 17 template<int N> struct T { 18 static_assert(N == 2, "N is not 2!"); // expected-error {{static_assert failed "N is not 2!"}} 19 }; 20 21 T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}} 22 T<2> t2; 23 24 template<typename T> struct S { 25 static_assert(sizeof(T) > sizeof(char), "Type not big enough!"); // expected-error {{static_assert failed "Type not big enough!"}} 26 }; 27 28 S<char> s1; // expected-note {{in instantiation of template class 'S<char>' requested here}} 29 S<int> s2; 30 31 static_assert(false, L"\xFFFFFFFF"); // expected-error {{static_assert failed L"\xFFFFFFFF"}} 32 static_assert(false, u"\U000317FF"); // expected-error {{static_assert failed u"\U000317FF"}} 33 // FIXME: render this as u8"\u03A9" 34 static_assert(false, u8"Ω"); // expected-error {{static_assert failed u8"\316\251"}} 35 static_assert(false, L"\u1234"); // expected-error {{static_assert failed L"\x1234"}} 36 static_assert(false, L"\x1ff" "0\x123" "fx\xfffff" "goop"); // expected-error {{static_assert failed L"\x1FF""0\x123""fx\xFFFFFgoop"}} 37 38 template<typename T> struct AlwaysFails { 39 // Only give one error here. 40 static_assert(false, ""); // expected-error {{static_assert failed}} 41 }; 42 AlwaysFails<int> alwaysFails; 43 44 template<typename T> struct StaticAssertProtected { 45 static_assert(__is_literal(T), ""); // expected-error {{static_assert failed}} 46 static constexpr T t = {}; // no error here 47 }; 48 struct X { ~X(); }; 49 StaticAssertProtected<int> sap1; 50 StaticAssertProtected<X> sap2; // expected-note {{instantiation}} 51 52 static_assert(true); // expected-warning {{C++17 extension}} 53 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}} 54 55 56 // Diagnostics for static_assert with multiple conditions 57 template<typename T> struct first_trait { 58 static const bool value = false; 59 }; 60 61 template<> 62 struct first_trait<X> { 63 static const bool value = true; 64 }; 65 66 template<typename T> struct second_trait { 67 static const bool value = false; 68 }; 69 70 static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait<X>::value' "message"}} 71 72 namespace std { 73 74 template <class Tp, Tp v> 75 struct integral_constant { 76 static const Tp value = v; 77 typedef Tp value_type; 78 typedef integral_constant type; 79 }; 80 81 template <class Tp, Tp v> 82 const Tp integral_constant<Tp, v>::value; 83 84 typedef integral_constant<bool, true> true_type; 85 typedef integral_constant<bool, false> false_type; 86 87 template <class Tp> 88 struct is_const : public false_type {}; 89 template <class Tp> 90 struct is_const<Tp const> : public true_type {}; 91 92 // We do not define is_same in terms of integral_constant to check that both implementations are supported. 93 template <typename T, typename U> 94 struct is_same { 95 static const bool value = false; 96 }; 97 98 template <typename T> 99 struct is_same<T, T> { 100 static const bool value = true; 101 }; 102 103 } // namespace std 104 105 struct ExampleTypes { 106 using T = int; 107 using U = float; 108 }; 109 110 static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message"); 111 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}} 112 static_assert(std::is_const<ExampleTypes::T>::value, "message"); 113 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}} 114 115 struct BI_tag {}; 116 struct RAI_tag : BI_tag {}; 117 struct MyIterator { 118 using tag = BI_tag; 119 }; 120 struct MyContainer { 121 using iterator = MyIterator; 122 }; 123 template <class Container> 124 void foo() { 125 static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message"); 126 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}} 127 } 128 template void foo<MyContainer>(); 129 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}} 130 131 namespace ns { 132 template <typename T, int v> 133 struct NestedTemplates1 { 134 struct NestedTemplates2 { 135 template <typename U> 136 struct NestedTemplates3 : public std::is_same<T, U> {}; 137 }; 138 }; 139 } // namespace ns 140 141 template <typename T, typename U, int a> 142 void foo2() { 143 static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message"); 144 // expected-error@-1{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}} 145 } 146 template void foo2<int, float, 3>(); 147 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}} 148 149 template <class T> 150 void foo3(T t) { 151 static_assert(std::is_const<T>::value, "message"); 152 // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}} 153 static_assert(std::is_const<decltype(t)>::value, "message"); 154 // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}} 155 } 156 void callFoo3() { 157 foo3([]() {}); 158 // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }} 159 } 160 161 template <class T> 162 void foo4(T t) { 163 static_assert(std::is_const<typename T::iterator>::value, "message"); 164 // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}} 165 } 166 void callFoo4() { foo4(42); } 167 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}} 168