parlaylib icon indicating copy to clipboard operation
parlaylib copied to clipboard

Adding `construct()` to `parlay::allocator` causes errors

Open WhateverLiu opened this issue 1 year ago • 0 comments

This could be a C++ skill issue. In order to skip default initialization, I added the following member function inside struct allocator in alloc.h:

template <typename U, typename... Args>
void construct(U* ptr, Args&&... args) {
    if constexpr (std::is_nothrow_default_constructible<U>::value and
                    sizeof...(args) == 0)
      ::new(static_cast<void*>(ptr)) U;
    else
      ::new(static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
}

If the function parlay::group_by_key() is used, the following compile error pops up:

/internal/sequence_base.h:570:63: note:   cannot convert ‘alloc’ (type ‘parlay::allocator<std::byte>’) to type ‘parlay::allocator<double>&’
  570 |             std::allocator_traits<T_allocator_type>::construct(
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
  571 |               alloc, std::addressof(dest_buffer[i]), std::move(current_buffer[i]));
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Without adding construct() while checking the type match at compile time using e.g. #pragma message(), T_allocator_type and decltype(alloc) are still different at line 570 of /internal/sequence_base.h.

Is this a bug or am I missing something? What should be done to make it work?

Oh and if parlay::group_by_key() is not used, the construct() function works fine. By just declaring parlay::sequence<double> x(1e6), one can confirm that the sequence is filled with garbage bits.

WhateverLiu avatar Jul 18 '24 19:07 WhateverLiu