foonathan / memory

STL compatible C++ memory allocator library using a new RawAllocator concept that is similar to an Allocator but easier to use and write.

Home Page:https://memory.foonathan.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allocator propagation on string copy

athanase opened this issue · comments

Hello @foonathan,

This is more of a question than an issue. The snippet below gives me an error which seems wierd to me:

foonathan::memory::static_allocator_storage< 4096 > storage;

using static_pool_t = foonathan::memory::memory_pool< foonathan::memory::node_pool, foonathan::memory::static_block_allocator >;
static_pool_t static_pool( foonathan::memory::unordered_set_node_size< int >::value, 4096, storage );

foonathan::memory::string< static_pool_t > test( "super important test", static_pool );
auto test2 = test.substr( 3 );
In file included from ../../src/sandbox/main.cpp:17:
In file included from ../../thirdparty/memory/include/foonathan/memory/container.hpp:33:
../../thirdparty/memory/include/foonathan/memory/std_allocator.hpp:115:56: error: no matching constructor for initialization of 'foonathan::memory::std_allocator<char, foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>>::allocator_type' (aka 'memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>')
            std_allocator() noexcept : alloc_reference(allocator_type{})
                                                       ^             ~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/bits/basic_string.h:159:45: note: in instantiation of member function 'foonathan::memory::std_allocator<char, foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>>::std_allocator' requested here
        _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
                                                   ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/bits/basic_string.h:2841:16: note: in instantiation of member function 'std::basic_string<char, std::char_traits<char>, foonathan::memory::std_allocator<char, foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>>>::basic_string' requested here
      { return basic_string(*this,
               ^
../../src/sandbox/main.cpp:30:20: note: in instantiation of member function 'std::basic_string<char, std::char_traits<char>, foonathan::memory::std_allocator<char, foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>>>::substr' requested here
        auto test2 = test.substr( 3 );
                          ^
../../thirdparty/memory/include/foonathan/memory/memory_pool.hpp:94:13: note: candidate constructor not viable: requires single argument 'other', but no arguments were provided
            memory_pool(memory_pool&& other) noexcept
            ^
../../thirdparty/memory/include/foonathan/memory/memory_pool.hpp:48:15: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
        class memory_pool
              ^
../../thirdparty/memory/include/foonathan/memory/memory_pool.hpp:79:13: note: candidate constructor template not viable: requires at least 2 arguments, but 0 were provided
            memory_pool(std::size_t node_size, std::size_t block_size, Args&&... args)
            ^
In file included from ../../src/sandbox/main.cpp:17:
In file included from ../../thirdparty/memory/include/foonathan/memory/container.hpp:33:
../../thirdparty/memory/include/foonathan/memory/std_allocator.hpp:120:17: error: static_assert failed due to requirement '!allocator_storage<reference_storage<memory_pool<node_pool, static_block_allocator>>, no_mutex>::integral_constant<bool, true>::value' "default constructor must not be used for stateful allocators"
                static_assert(!alloc_reference::is_stateful::value,
                ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Doesn't the foonathan::memory::std_allocator propagates the allocator automatically in this case ?

No, not in this case, due to the behavior of std::string::substr(): https://eel.is/c++draft/string.substr
It forwards to his constructor overload, which takes pointer + size and tries to default constructor an allocator. This is not possible with a memory pool.

I'm afraid you cannot use substr().