brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · f1e5af6 Raw
56 lines · cpp
1// RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s2// RUN: %clang_cc1 -std=c++2c  -verify=ref,both %s3 4 5/// This example used to cause an invalid read because allocating6/// an array needs to return a pointer to the first element,7/// not to the array.8 9namespace std {10  using size_t = decltype(sizeof(0));11 12  template <class _Tp>13  class allocator {14  public:15    typedef size_t size_type;16    typedef _Tp value_type;17    constexpr _Tp *allocate(size_t __n) {18      return static_cast<_Tp *>(::operator new(__n * sizeof(_Tp)));19    }20  };21}22 23void *operator new(std::size_t, void *p) { return p; }24void* operator new[] (std::size_t, void* p) {return p;}25 26namespace std {27  template <class _Ep>28  class initializer_list {29    const _Ep *__begin_;30    __SIZE_TYPE__ __size_;31 32  public:33    typedef _Ep value_type;34    typedef const _Ep &reference;35    constexpr __SIZE_TYPE__ size() const noexcept { return __size_; }36    constexpr const _Ep *begin() const noexcept { return __begin_; }37    constexpr const _Ep *end() const noexcept { return __begin_ + __size_; }38  };39}40 41template<typename T>42class vector {43public:44  constexpr vector(std::initializer_list<T> Ts) {45    A = B = std::allocator<T>{}.allocate(Ts.size()); // both-note {{heap allocation performed here}}46 47    new (A) T(*Ts.begin());48  }49private:50  T *A = nullptr;51  T *B = nullptr;52};53 54constexpr vector<vector<int>> ints = {{3}, {4}}; // both-error {{must be initialized by a constant expression}} \55                                                 // both-note {{pointer to}}56