Skip to content

Latest commit

 

History

History
124 lines (94 loc) · 2.49 KB

193.md

File metadata and controls

124 lines (94 loc) · 2.49 KB
Info

Example

constexpr auto foo() {
  auto p = new int{42};
  delete p;
  return true;
}

constexpr auto bar() {
  auto p = new int{42};
  //delete p;
  return false;
}

static_assert(foo());
static_assert(bar()); // compilation error - allocation performed here was not deallocated

https://godbolt.org/z/oPd7zY

Puzzle

  • Can you implement a simplified version of unique_ptr which will take advantage of constexpr new?
/*TODO - constexpr unique_ptr */

template<class T>
constexpr auto foo(T value) {
  return unique_ptr{new T(value)};
}

static_assert(std::is_same_v<unique_ptr<int>, decltype(foo(int{}))>);
static_assert(std::is_same_v<unique_ptr<double>, decltype(foo(double{}))>);

static_assert(0 == *foo(0));
static_assert(42 == *foo(42));

static_assert(4.2f == *foo(4.2f));
static_assert(42.d == *foo(42.d));

https://godbolt.org/z/rPb85a

Solutions

template<class T>
class unique_ptr {
public:
    constexpr unique_ptr(T* ptr) : ptr(ptr) {}
    constexpr T operator*() { return *ptr; }
    constexpr ~unique_ptr() { delete ptr; }
private:
    T* ptr = nullptr;
};

https://godbolt.org/z/z6PcME

template <typename T>
struct unique_ptr {
    constexpr unique_ptr(T* p) : ptr_(p) {}
    constexpr ~unique_ptr() { delete ptr_; }
    constexpr const auto& operator*() const { return *ptr_; }

private:
    T* ptr_;
};

https://godbolt.org/z/rh5r5a

template<typename T>
class unique_ptr
{
    public:
        constexpr unique_ptr(T* value) : _value(value) {}
        constexpr T& operator*() {return *_value;}
        constexpr ~unique_ptr() {delete _value;}
    private:
        T* _value;
};

https://godbolt.org/z/fMEKTo

template <typename T>
class unique_ptr {
    T* ptr;
public:
    constexpr unique_ptr(T* ptr) : ptr(ptr) {}
    constexpr std::add_lvalue_reference_t<T> operator*() { return *ptr; }
    constexpr ~unique_ptr() { delete ptr; }
};

https://godbolt.org/z/cGs9PG

template <typename T>
struct unique_ptr {
  constexpr explicit(true) unique_ptr(T *arg) : val(arg) {}
  constexpr ~unique_ptr() { delete val; }

  [[nodiscard]] constexpr T operator*() const noexcept { return *val; }

  T *val;
};

https://godbolt.org/z/v1abT6