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

foonathan::memory as fmt custom allocator

athanase opened this issue · comments

Hello @foonathan,

I am trying to use foonathan::memory as the custom allocator for the fmt library, but it does not work.
See the snippet below for an example (chosen storage is just for the sake of the example) :

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( 16, 4096, storage );

using custom_memory_buffer = fmt::basic_memory_buffer< char, fmt::inline_buffer_size, foonathan::memory::std_allocator< char, static_pool_t > >;

using custom_string = std::basic_string< char, std::char_traits< char >, foonathan::memory::std_allocator< char, static_pool_t > >;

custom_string vformat( static_pool_t alloc, fmt::string_view format_str, fmt::format_args args )
{
	custom_memory_buffer buf( alloc );
	fmt::vformat_to( buf, format_str, args );
	return custom_string( buf.data(), buf.size(), alloc );
}

template< typename... Args >
inline custom_string format( foonathan::memory::std_allocator< char, static_pool_t > alloc, fmt::string_view format_str, const Args&... args )
{
	return vformat( alloc, format_str, fmt::make_format_args( args... ) );
}

And I get the following error message:

In file included from ../src/Format.cpp:9:
../src/Format.hpp:44:2: error: no matching function for call to 'vformat_to'
        fmt::vformat_to( buf, format_str, args );
        ^~~~~~~~~~~~~~~
../thirdparty/fmt/include/fmt/core.h:2892:6: note: candidate template ignored: requirement 'detail::is_output_iterator<fmt::basic_memory_buffer<char, 500, foonathan::memory::std_allocator<char, foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>>>, char, void>::value' was not satisfied [with OutputIt = fmt::basic_memory_buffer<char, 500, foonathan::memory::std_allocator<char, foonathan::memory::memory_pool<foonathan::memory::node_pool, foonathan::memory::static_block_allocator>>>]
auto vformat_to(OutputIt out, string_view fmt, format_args args) -> OutputIt {
     ^
../thirdparty/fmt/include/fmt/format.h:2827:6: note: candidate function template not viable: requires 4 arguments, but 3 were provided
auto vformat_to(OutputIt out, const Locale& loc, string_view fmt,
     ^
1 error generated.

This is unrelated to foonathan::memory; you should get the same error if you use std::allocator as allocator. I believe the issue is that there is no fmt::vformat_to() that takes a buffer, you need to use std::back_inserter(buffer) instead.