Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,28 @@ struct {
} inline shared;

template<typename T>
[[nodiscard]] auto cpp2_new(auto&& ...args) -> std::unique_ptr<T> {
return unique.cpp2_new<T>(CPP2_FORWARD(args)...);
class owning_reference {
public:
explicit owning_reference(std::unique_ptr<T>&& ptr):_ptr(cpp2::impl::assert_not_null(std::move(ptr))){}
~owning_reference() = default;
owning_reference(const owning_reference& ref) = delete;
owning_reference& operator=(const owning_reference& ref) noexcept = delete;
owning_reference(owning_reference&& ref) = default;
owning_reference& operator=(owning_reference&& ref) noexcept = default;
typename std::add_lvalue_reference<T>::type operator*() const { return *cpp2::impl::assert_not_null(_ptr.get()); }
typename std::add_lvalue_reference<T>::type operator->() const noexcept {return *cpp2::impl::assert_not_null(_ptr.get());}
auto get() const noexcept { return _ptr.get();}
private:
std::unique_ptr<T> _ptr;
};

template<typename T>
[[nodiscard]] auto cpp2_new(auto&& ...args) -> std::optional<owning_reference<T>> {
auto _ptr = unique.cpp2_new<T>(CPP2_FORWARD(args)...);
if (_ptr) {
return owning_reference<T>{std::move(_ptr)};
}
return {};
}


Expand Down
2,505 changes: 2,505 additions & 0 deletions regression-tests/clang++-15.patch

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion regression-tests/mixed-type-safety-1.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ main: () -> int =
print( "1 is int? ", 1 is int );

c := new<Circle>(); // safe by construction
s : * Shape = c.get(); // safe by Lifetime
s : * Shape = c*.get(); // safe by Lifetime
print("\ns* is Shape? ", s* is Shape );
print( "s* is Circle? ", s* is Circle );
print( "s* is Square? ", s* is Square );
Expand Down
7 changes: 6 additions & 1 deletion regression-tests/pure2-intro-example-three-loops.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ main: () -> int = {
( "hello", "big", "world" );
view: std::span<std::string> = words;

i := new<int>(0);
p_i := new<int>(0);
if (!p_i) {
return 1;
}
i := (move p_i*);

while i* < view.ssize() next i*++ {
print( view[i*] );
}
Expand Down
Loading