[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / single-element-init-list.cpp
blob33d986e08401383bc52c63ec4a3a8bb3d791d28b
1 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
3 // This is heavily affected by the speculative resolution applied to CWG2311
4 // So behaviour shown here is subject to change.
6 // expected-no-diagnostics
8 namespace std {
9 typedef decltype(sizeof(int)) size_t;
11 // libc++'s implementation
12 template <class _E>
13 class initializer_list
15 const _E* __begin_;
16 size_t __size_;
18 initializer_list(const _E* __b, size_t __s)
19 : __begin_(__b),
20 __size_(__s)
23 public:
24 typedef _E value_type;
25 typedef const _E& reference;
26 typedef const _E& const_reference;
27 typedef size_t size_type;
29 typedef const _E* iterator;
30 typedef const _E* const_iterator;
32 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
34 constexpr size_t size() const {return __size_;}
35 const _E* begin() const {return __begin_;}
36 const _E* end() const {return __begin_ + __size_;}
39 template<typename T>
40 struct vector {
41 size_t sz;
42 constexpr vector() : sz(0) {}
43 constexpr vector(initializer_list<T> ilist) : sz(ilist.size()) {}
44 constexpr vector(const vector& other) : sz(other.sz) {}
45 constexpr std::size_t size() const { return sz; }
49 // https://github.com/llvm/llvm-project/pull/77768#issuecomment-1908062472
50 namespace Issue1 {
51 struct A {
52 constexpr A() {}
55 struct B {
56 int called_ctor;
57 constexpr explicit B(A) : called_ctor(0) {}
58 constexpr explicit B(std::vector<A>) : called_ctor(1) {}
61 struct C {
62 B b;
63 constexpr C() : b({A()}) {}
66 static_assert(C().b.called_ctor == 0);
69 // https://github.com/llvm/llvm-project/pull/77768#issuecomment-1957171805
70 namespace Issue2 {
71 struct A {
72 constexpr A(int x_) {}
73 constexpr A(const std::vector<A>& a) {}
76 void f() {
77 constexpr std::vector<A> a{1,2};
78 constexpr std::vector<A> b{a};
79 // -> constexpr std::vector<A> b(std::initializer_list<A>{ A(a) });
80 static_assert(b.size() == 1);