Should json obj assignment to parent object work for emplace vs. [] #5106
Replies: 2 comments 1 reply
|
Here is an overview of your options: https://json.nlohmann.me/features/element_access/ |
|
The std::pair<iterator, bool> emplace(const key_type& key, T&& t);where So either pass an rvalue: parentObj.emplace("child", std::move(childObj)); // childObj is moved from after this
parentObj.emplace("child", Json(childObj)); // explicit copyor keep |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
While running clang-tidy on my code that uses the excellent nlohmann Json it complains that using [] is unsafe
"possibly unsafe operator[]', consider bounds-safe alternatives
Below is test case showing what I am seeing. For simple json primitives emplace compiles and works equivalently to operator[].
Apologies if I am incorrectly expecting it to work or using the wrong method.
compiled with g++ -Wall -o jsontest jsontest.cpp
jsontest.cpp
==============
All reactions