Skip to content

Commit 687e68c

Browse files
authored
Merge pull request #1281 from SekaiArendelle/constexpr-deque
[deque] fix: constexpr support for deque in C++23
2 parents 66a34da + 44a1f6f commit 687e68c

5 files changed

Lines changed: 539 additions & 43 deletions

File tree

include/fast_io_core_impl/allocation/adapters.h

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#pragma once
1+
#pragma once
22

33
// To make a constexpr allocator, we need ::std::allocator. Because only new expression and
44
// ::std::allocator<T>::allocate are allowed in constexpr functions. See https://github.com/microsoft/STL/issues/1532
@@ -145,10 +145,16 @@ class generic_allocator_adapter
145145
if (false)
146146
#endif
147147
{
148-
auto p{::operator new(n)};
148+
auto p{
149+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
150+
__builtin_operator_new(n)
151+
#else
152+
::operator new(n)
153+
#endif
154+
};
149155
if (zero)
150156
{
151-
::fast_io::freestanding::bytes_clear_n(reinterpret_cast<::std::byte *>(p), n);
157+
::fast_io::freestanding::bytes_clear_n(static_cast<::std::byte *>(p), n);
152158
}
153159
return p;
154160
}
@@ -682,7 +688,11 @@ class generic_allocator_adapter
682688
#if __cpp_constexpr_dynamic_alloc >= 201907L
683689
if (__builtin_is_constant_evaluated())
684690
{
691+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_delete)
692+
__builtin_operator_delete(p);
693+
#else
685694
::operator delete(p);
695+
#endif
686696
}
687697
else
688698
#endif
@@ -704,7 +714,11 @@ class generic_allocator_adapter
704714
#if __cpp_constexpr_dynamic_alloc >= 201907L
705715
if (__builtin_is_constant_evaluated())
706716
{
717+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_delete)
718+
__builtin_operator_delete(p);
719+
#else
707720
::operator delete(p);
721+
#endif
708722
}
709723
else
710724
#endif
@@ -747,10 +761,16 @@ class generic_allocator_adapter
747761
if (false)
748762
#endif
749763
{
750-
auto p{::operator new(n)};
764+
auto p{
765+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
766+
__builtin_operator_new(n)
767+
#else
768+
::operator new(n)
769+
#endif
770+
};
751771
if (zero)
752772
{
753-
::fast_io::freestanding::bytes_clear_n(reinterpret_cast<::std::byte *>(p), n);
773+
::fast_io::freestanding::bytes_clear_n(static_cast<::std::byte *>(p), n);
754774
}
755775
return p;
756776
}
@@ -847,7 +867,13 @@ class generic_allocator_adapter
847867
if (false)
848868
#endif
849869
{
850-
return ::operator new(n);
870+
return
871+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
872+
__builtin_operator_new(n)
873+
#else
874+
::operator new(n)
875+
#endif
876+
;
851877
}
852878
if constexpr (::fast_io::details::has_allocate_aligned_impl<alloc>)
853879
{
@@ -874,7 +900,13 @@ class generic_allocator_adapter
874900
if (false)
875901
#endif
876902
{
877-
return ::operator new(n);
903+
return
904+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
905+
__builtin_operator_new(n)
906+
#else
907+
::operator new(n)
908+
#endif
909+
;
878910
}
879911
if constexpr (::fast_io::details::has_allocate_aligned_zero_impl<alloc>)
880912
{
@@ -904,7 +936,13 @@ class generic_allocator_adapter
904936
if (false)
905937
#endif
906938
{
907-
return {::operator new(n), n};
939+
return {
940+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
941+
__builtin_operator_new(n)
942+
#else
943+
::operator new(n)
944+
#endif
945+
, n};
908946
}
909947
else
910948
{
@@ -931,7 +969,13 @@ class generic_allocator_adapter
931969
if (false)
932970
#endif
933971
{
934-
return {::operator new(n), n};
972+
return {
973+
#if FAST_IO_HAS_BUILTIN(__builtin_operator_new)
974+
__builtin_operator_new(n)
975+
#else
976+
::operator new(n)
977+
#endif
978+
, n};
935979
}
936980
else
937981
{
@@ -2062,7 +2106,7 @@ class generic_allocator_adapter
20622106
if (false)
20632107
#endif
20642108
{
2065-
return ::operator new(n);
2109+
return static_cast<void *>(::fast_io::freestanding::allocator<::std::byte>{}.allocate(n));
20662110
}
20672111
else
20682112
{

include/fast_io_core_impl/freestanding/algorithm.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,21 @@ inline constexpr output_iter overlapped_copy_trivial(input_iter first, ::std::si
301301
{
302302
tempbufferptr[i] = first[i];
303303
}
304-
for (::std::size_t i{}; i != n; ++i)
304+
#if __cpp_if_consteval >= 202106L
305+
if consteval
306+
{
307+
for (::std::size_t i{}; i != n; ++i)
308+
{
309+
::std::construct_at(::std::addressof(result[i]), ::std::move(tempbufferptr[i]));
310+
}
311+
}
312+
else
313+
#endif
305314
{
306-
result[i] = ::std::move(tempbufferptr[i]);
315+
for (::std::size_t i{}; i != n; ++i)
316+
{
317+
result[i] = ::std::move(tempbufferptr[i]);
318+
}
307319
}
308320
return result + n;
309321
}

0 commit comments

Comments
 (0)