AK: Make smart pointer factories work with aggregates

Aggregate initialization with brace-enclosed parameters is a
[C++20 feature][1] not yet implemented by Clang. This caused compile
errors if we tried to use the factory functions to create smart pointers
to aggregates.

As a (temporary) fix, [the LWG's previously proposed solution][2] is
implemented by this commit.

Now, wherever it's not possible to direct-initialize, aggregate
initialization is performed.

[1]:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0960r3.html
[2]: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2089
This commit is contained in:
Daniel Bertalan 2021-07-01 10:21:14 +02:00 committed by Ali Mohammad Pur
parent fda9f394d1
commit 3c6bdb8a61
Notes: sideshowbarker 2024-07-18 11:05:26 +09:00
5 changed files with 37 additions and 4 deletions

View file

@ -163,11 +163,18 @@ inline NonnullOwnPtr<T> adopt_own(T& object)
#endif
template<class T, class... Args>
inline NonnullOwnPtr<T> make(Args&&... args)
requires(IsConstructible<T, Args...>) inline NonnullOwnPtr<T> make(Args&&... args)
{
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T(forward<Args>(args)...));
}
// FIXME: Remove once P0960R3 is available in Clang.
template<class T, class... Args>
inline NonnullOwnPtr<T> make(Args&&... args)
{
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T { forward<Args>(args)... });
}
template<typename T>
struct Traits<NonnullOwnPtr<T>> : public GenericTraits<NonnullOwnPtr<T>> {
using PeekType = T*;

View file

@ -336,11 +336,17 @@ inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b)
}
template<typename T, class... Args>
inline NonnullRefPtr<T> create(Args&&... args)
requires(IsConstructible<T, Args...>) inline NonnullRefPtr<T> create(Args&&... args)
{
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T(forward<Args>(args)...));
}
// FIXME: Remove once P0960R3 is available in Clang.
template<typename T, class... Args>
inline NonnullRefPtr<T> create(Args&&... args)
{
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T { forward<Args>(args)... });
}
}
template<typename T>

View file

@ -206,11 +206,19 @@ inline OwnPtr<T> adopt_own_if_nonnull(T* object)
}
template<typename T, class... Args>
inline OwnPtr<T> try_make(Args&&... args)
requires(IsConstructible<T, Args...>) inline OwnPtr<T> try_make(Args&&... args)
{
return adopt_own_if_nonnull(new (nothrow) T(forward<Args>(args)...));
}
// FIXME: Remove once P0960R3 is available in Clang.
template<typename T, class... Args>
inline OwnPtr<T> try_make(Args&&... args)
{
return adopt_own_if_nonnull(new (nothrow) T { forward<Args>(args)... });
}
template<typename T>
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
using PeekType = T*;

View file

@ -486,11 +486,18 @@ inline RefPtr<T> adopt_ref_if_nonnull(T* object)
}
template<typename T, class... Args>
inline RefPtr<T> try_create(Args&&... args)
requires(IsConstructible<T, Args...>) inline RefPtr<T> try_create(Args&&... args)
{
return adopt_ref_if_nonnull(new (nothrow) T(forward<Args>(args)...));
}
// FIXME: Remove once P0960R3 is available in Clang.
template<typename T, class... Args>
inline RefPtr<T> try_create(Args&&... args)
{
return adopt_ref_if_nonnull(new (nothrow) T { forward<Args>(args)... });
}
}
using AK::adopt_ref_if_nonnull;

View file

@ -443,6 +443,10 @@ auto declval() -> T;
template<typename T, typename... Args>
inline constexpr bool IsCallableWithArguments = requires(T t) { t(declval<Args>()...); };
template<typename T, typename... Args>
inline constexpr bool IsConstructible = requires { ::new T(declval<Args>()...); };
}
using AK::Detail::AddConst;
using AK::Detail::Conditional;
@ -459,6 +463,7 @@ using AK::Detail::IsBaseOf;
using AK::Detail::IsCallableWithArguments;
using AK::Detail::IsClass;
using AK::Detail::IsConst;
using AK::Detail::IsConstructible;
using AK::Detail::IsEnum;
using AK::Detail::IsFloatingPoint;
using AK::Detail::IsFunction;