Replies: 3 comments 1 reply
-
Can you share more information? Compiler errors? |
Beta Was this translation helpful? Give feedback.
-
@nlohmann Thanks for your quick reply. There is no constructor function like basic_json( Allocator& alloc )) , the alloc is not passed to children elements. when using gcc 10.3, the compile error message is:
when use Vistual studio 2022, the translated error message is:
|
Beta Was this translation helpful? Give feedback.
-
Hi @nlohmann, I was trying to use the library with However, when I tried adding values to the JSON object, I encountered a static assertion failure. After investigating, I found that Note: The full error message is quite large, so I'm not pasting it here. I've attached a screenshot showing the last lines of the error output for reference. Minimal reproducible example:#include <iostream>
#include <memory_resource>
#include <nlohmann/json.hpp>
// Template alias to pass polymorphic allocators to nlohmann::json
#if 1
template<typename Key, typename T, typename Ignored, typename Allocator>
using pmr_compatible_map = std::map<Key, T, std::less<Key>, Allocator>;
template<typename T, typename Allocator>
using pmr_compatible_vector = std::vector<T, Allocator>;
using pmr_string = std::basic_string<char, std::char_traits<char>, std::pmr::polymorphic_allocator<char>>;
#endif
using json_pmr = nlohmann::basic_json<
pmr_compatible_map,
pmr_compatible_vector,
pmr_string,
bool,
int64_t,
uint64_t,
double,
std::pmr::polymorphic_allocator>;
int main()
{
std::pmr::monotonic_buffer_resource arena(10240);
std::pmr::polymorphic_allocator<char> alloc(&arena);
// Create a polymorphic string using the allocator
pmr_string str(alloc);
str = "Hello, PMR world!";
std::cout << str << std::endl;
json_pmr obj;
obj["name"] = "Ankit Ghevariya"; // ❌ Static assertion failure here: no allocator was passed to the object
return 0;
} In summary:
Environment
@nlohmann — is there currently any recommended or supported way to safely use polymorphic allocators ( Thank You! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We are using C++20 in embedded system with 128MB memory, so we are worrying about the dynamical memory allocation/memory segmentation. And want to use pmr and customize json library as :
It seems that this feature is not fully finished.
Beta Was this translation helpful? Give feedback.
All reactions