How to use JsonDocuments with std::unique_ptrs?
Created by: matth-x
Hi Benoît,
Thank you for all the work on your library, it's really awesome and helps me a lot with my own library (if you're curious, you can have a look: https://github.com/matth-x/ArduinoOcpp).
As your design allows to build the JSON documents as a composite, I took full advantage of it. To make the memory management still straightforward despite multiple sub-documents per document, I would like to go on using unique_ptr
s because of its familiar semantics.
But the following way of using them seems problematic:
std::unique_ptr<DynamicJsonDocument> doc = factory->makeDoc1_uniquePtr();
//...
if (specialCase) {
doc = factory->makeDoc2_uniquePtr(); //compiler error
}
This gives me the following compiler output:
~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\bits\unique_ptr.h: In instantiation of 'void std::unique_ptr<_Tp, _Dp>::reset(std::unique_ptr<_Tp, _Dp>::pointer) [with _Tp = ArduinoJson6185_91::BasicJsonDocument; _Dp = std::default_delete >; std::unique_ptr<_Tp, _Dp>::pointer = ArduinoJson6185_91::BasicJsonDocument*]':
~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\bits\unique_ptr.h:251:7:
required from 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = ArduinoJson6185_91::BasicJsonDocument; _Dp = std::default_delete >]'
src/mytest.cpp:102:10: required from here
~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\bits\unique_ptr.h:342:6:
error:** call of overloaded 'swap(ArduinoJson6185_91::BasicJsonDocument*&, ArduinoJson6185_91::BasicJsonDocument*&)' is ambiguous
swap(std::get<0>(_M_t), __p);
^
In file included from ~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_pair.h:59:0,
from ~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:64,
from ~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\deque:60,
from src/mytest.h:8,
from src/mytest.cpp:5:
~\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0\bits\move.h:176:5: note:
candidate: void std::swap(_Tp&, _Tp&) [with _Tp = ArduinoJson6185_91::BasicJsonDocument*]
swap(_Tp& __a, _Tp& __b)
^
In file included from .pio/libdeps/esp32-development-board/ArduinoJson/src/ArduinoJson/MsgPack/endianess.hpp:8:0,
from .pio/libdeps/esp32-development-board/ArduinoJson/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp:9,
from .pio/libdeps/esp32-development-board/ArduinoJson/src/ArduinoJson.hpp:37,
from .pio/libdeps/esp32-development-board/ArduinoJson/src/ArduinoJson.h:9,
from src/mytest.h:10,
from src/mytest.cpp:5:
.pio/libdeps/esp32-development-board/ArduinoJson/src/ArduinoJson/Polyfills/utility.hpp:11:13: note: candidate: void ArduinoJson6185_91::swap(T&, T&) [with T = ArduinoJson6185_91::BasicJsonDocument*]
inline void swap(T& a, T& b) {
^
*** [.pio\build\esp32-development-board\src\mytest.cpp.o] Error 1
The problem seems to be due to the implementation of the operator=
in the unique_ptr
triggering a call to swap
which could apply to both std::swap
and ARDUINOJSON_NAMESPACE::swap
as a consequence of ADL.
What is the right way of using JsonDocuments with unique_ptr
s? Have I mistaken something? Thank you in advance for tips!