1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -pedantic -triple=x86_64-linux-gnu
3 int f(); // expected-note {{declared here}}
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"}}
10 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
14 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
17 template<int N
> struct T
{
18 static_assert(N
== 2, "N is not 2!"); // expected-error {{static_assert failed due to requirement '1 == 2' "N is not 2!"}}
21 T
<1> t1
; // expected-note {{in instantiation of template class 'T<1>' requested here}}
24 template<typename T
> struct S
{
25 static_assert(sizeof(T
) > sizeof(char), "Type not big enough!"); // expected-error {{static_assert failed due to requirement 'sizeof(char) > sizeof(char)' "Type not big enough!"}}
28 S
<char> s1
; // expected-note {{in instantiation of template class 'S<char>' requested here}}
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"}}
38 template<typename T
> struct AlwaysFails
{
39 // Only give one error here.
40 static_assert(false, ""); // expected-error {{static_assert failed}}
42 AlwaysFails
<int> alwaysFails
;
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
49 StaticAssertProtected
<int> sap1
;
50 StaticAssertProtected
<X
> sap2
; // expected-note {{instantiation}}
52 static_assert(true); // expected-warning {{C++17 extension}}
53 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}}
56 // Diagnostics for static_assert with multiple conditions
57 template<typename T
> struct first_trait
{
58 static const bool value
= false;
62 struct first_trait
<X
> {
63 static const bool value
= true;
66 template<typename T
> struct second_trait
{
67 static const bool value
= false;
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"}}
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 constexpr operator value_type() const noexcept
{ return value
; }
80 constexpr value_type
operator()() const noexcept
{ return value
; }
83 template <class Tp
, Tp v
>
84 const Tp integral_constant
<Tp
, v
>::value
;
86 typedef integral_constant
<bool, true> true_type
;
87 typedef integral_constant
<bool, false> false_type
;
90 struct is_const
: public false_type
{};
92 struct is_const
<Tp
const> : public true_type
{};
94 // We do not define is_same in terms of integral_constant to check that both implementations are supported.
95 template <typename T
, typename U
>
97 static const bool value
= false;
100 template <typename T
>
101 struct is_same
<T
, T
> {
102 static const bool value
= true;
107 struct ExampleTypes
{
108 explicit ExampleTypes(int);
113 static_assert(std::is_same
<ExampleTypes::T
, ExampleTypes::U
>::value
, "message");
114 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}}
115 static_assert(std::is_const
<ExampleTypes::T
>::value
, "message");
116 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}}
117 static_assert(!std::is_const
<const ExampleTypes::T
>::value
, "message");
118 // expected-error@-1{{static_assert failed due to requirement '!std::is_const<const int>::value' "message"}}
119 static_assert(!(std::is_const
<const ExampleTypes::T
>::value
), "message");
120 // expected-error@-1{{static_assert failed due to requirement '!(std::is_const<const int>::value)' "message"}}
121 static_assert(std::is_const
<const ExampleTypes::T
>::value
== false, "message");
122 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<const int>::value == false' "message"}}
123 static_assert(!(std::is_const
<const ExampleTypes::T
>::value
== true), "message");
124 // expected-error@-1{{static_assert failed due to requirement '!(std::is_const<const int>::value == true)' "message"}}
125 static_assert(std::is_const
<ExampleTypes::T
>(), "message");
126 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>()' "message"}}
127 static_assert(!(std::is_const
<const ExampleTypes::T
>()()), "message");
128 // expected-error@-1{{static_assert failed due to requirement '!(std::is_const<const int>()())' "message"}}
129 static_assert(std::is_same
<decltype(std::is_const
<const ExampleTypes::T
>()), int>::value
, "message");
130 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<std::is_const<const int>, int>::value' "message"}}
131 static_assert(std::is_const
<decltype(ExampleTypes::T(3))>::value
, "message");
132 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}}
133 static_assert(std::is_const
<decltype(ExampleTypes::T())>::value
, "message");
134 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}}
135 static_assert(std::is_const
<decltype(ExampleTypes(3))>::value
, "message");
136 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<ExampleTypes>::value' "message"}}
139 struct RAI_tag
: BI_tag
{};
144 using iterator
= MyIterator
;
146 template <class Container
>
148 static_assert(std::is_same
<RAI_tag
, typename
Container::iterator::tag
>::value
, "message");
149 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}}
151 template void foo
<MyContainer
>();
152 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}}
155 template <typename T
, int v
>
156 struct NestedTemplates1
{
157 struct NestedTemplates2
{
158 template <typename U
>
159 struct NestedTemplates3
: public std::is_same
<T
, U
> {};
164 template <typename T
, typename U
, int a
>
166 static_assert(::ns::NestedTemplates1
<T
, a
>::NestedTemplates2::template NestedTemplates3
<U
>::value
, "message");
167 // expected-error@-1{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}}
169 template void foo2
<int, float, 3>();
170 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}}
174 static_assert(std::is_const
<T
>::value
, "message");
175 // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}}
176 static_assert(std::is_const
<decltype(t
)>::value
, "message");
177 // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}}
181 // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }}
186 static_assert(std::is_const
<typename
T::iterator
>::value
, "message");
187 // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}}
189 void callFoo4() { foo4(42); }
190 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}}
192 static_assert(42, "message");
193 static_assert(42.0, "message"); // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 42 to true}}
194 constexpr int *p
= 0;
195 static_assert(p
, "message"); // expected-error {{static_assert failed}}
199 constexpr NotBool constexprNotBool
;
200 static_assert(notBool
, "message"); // expected-error {{value of type 'struct NotBool' is not contextually convertible to 'bool'}}
201 static_assert(constexprNotBool
, "message"); // expected-error {{value of type 'const NotBool' is not contextually convertible to 'bool'}}