q20::construct_at(): fix various issues

Fix several issues in 72c2cdbc572f8b8b45a57a451e2bc19bb1c53b0c,
which I was too slow to review before it went in:

- use the correct feature macro, not __cplusplus
- use the correct signature (return T*, not void)
- don't make the function static
- add a comment mentioning the material difference to std::construct_at
- drop unneeded <qxptype_traits.h> include

Task-number: QTBUG-109394
Change-Id: I39d1908f565b1c1a31d5741924ac173447ec9057
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 67ed712235375fa1d1b9ea0e5c71755886f65f83)
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-02-02 07:40:08 +01:00
parent e4ef4fd3a6
commit e29d578066

View File

@ -4,9 +4,12 @@
#ifndef Q20MEMORY_H
#define Q20MEMORY_H
#include <QtCore/qxptype_traits.h>
#include <QtCore/qtconfigmacros.h>
#include <memory>
#include <utility>
#include <type_traits>
//
// W A R N I N G
@ -26,19 +29,20 @@
QT_BEGIN_NAMESPACE
// like std::construct_at (but not whitelisted for constexpr)
namespace q20 {
#if __cplusplus >= 202002L
#ifdef __cpp_lib_constexpr_dynamic_alloc
using std::construct_at;
#else
template <typename T,
typename... Args,
typename Enable = std::void_t<decltype(::new (std::declval<void *>()) T(std::declval<Args>()...))> >
static void construct_at(T *ptr, Args && ... args)
T *construct_at(T *ptr, Args && ... args)
{
::new (const_cast<void*>(static_cast<const volatile void*>(ptr))) T(std::forward<Args>(args)...);
return ::new (const_cast<void *>(static_cast<const volatile void *>(ptr)))
T(std::forward<Args>(args)...);
}
#endif
#endif // __cpp_lib_constexpr_dynamic_alloc
} // namespace q20
QT_END_NAMESPACE