summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/static-assert.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/test/SemaCXX/static-assert.cpp')
-rw-r--r--clang/test/SemaCXX/static-assert.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/static-assert.cpp b/clang/test/SemaCXX/static-assert.cpp
index 846303807a0..38f82091ae7 100644
--- a/clang/test/SemaCXX/static-assert.cpp
+++ b/clang/test/SemaCXX/static-assert.cpp
@@ -68,3 +68,100 @@ template<typename T> struct second_trait {
};
static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait<X>::value' "message"}}
+
+namespace std {
+
+template <class Tp, Tp v>
+struct integral_constant {
+ static const Tp value = v;
+ typedef Tp value_type;
+ typedef integral_constant type;
+};
+
+template <class Tp, Tp v>
+const Tp integral_constant<Tp, v>::value;
+
+typedef integral_constant<bool, true> true_type;
+typedef integral_constant<bool, false> false_type;
+
+template <class Tp>
+struct is_const : public false_type {};
+template <class Tp>
+struct is_const<Tp const> : public true_type {};
+
+// We do not define is_same in terms of integral_constant to check that both implementations are supported.
+template <typename T, typename U>
+struct is_same {
+ static const bool value = false;
+};
+
+template <typename T>
+struct is_same<T, T> {
+ static const bool value = true;
+};
+
+} // namespace std
+
+struct ExampleTypes {
+ using T = int;
+ using U = float;
+};
+
+static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message");
+// expected-error@-1{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}}
+static_assert(std::is_const<ExampleTypes::T>::value, "message");
+// expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}}
+
+struct BI_tag {};
+struct RAI_tag : BI_tag {};
+struct MyIterator {
+ using tag = BI_tag;
+};
+struct MyContainer {
+ using iterator = MyIterator;
+};
+template <class Container>
+void foo() {
+ static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message");
+ // expected-error@-1{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}}
+}
+template void foo<MyContainer>();
+// expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}}
+
+namespace ns {
+template <typename T, int v>
+struct NestedTemplates1 {
+ struct NestedTemplates2 {
+ template <typename U>
+ struct NestedTemplates3 : public std::is_same<T, U> {};
+ };
+};
+} // namespace ns
+
+template <typename T, typename U, int a>
+void foo2() {
+ static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message");
+ // expected-error@-1{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}}
+}
+template void foo2<int, float, 3>();
+// expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}}
+
+template <class T>
+void foo3(T t) {
+ static_assert(std::is_const<T>::value, "message");
+ // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}}
+ static_assert(std::is_const<decltype(t)>::value, "message");
+ // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}}
+}
+void callFoo3() {
+ foo3([]() {});
+ // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }}
+}
+
+template <class T>
+void foo4(T t) {
+ static_assert(std::is_const<typename T::iterator>::value, "message");
+ // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}}
+}
+void callFoo4() { foo4(42); }
+// expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}}
OpenPOWER on IntegriCloud