Closed
Description
I'm trying to read into a vector, but run into the following problem:
#include <vector>
#include <json.hpp>
void from_json(const nlohmann::json &j, std::vector<int> &v) { /* something */ }
int main() {
nlohmann::json j;
std::vector<int> v1 = j; // compiles
std::vector<int> v2;
v2 = j; // does not compile
}
Implementing as adl_serializer
does not seem to work. Any help is greatly appreciated!
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
mlund commentedon Jul 27, 2017
Perhaps I should mention that
int
is used for the sake of clarity, and that it's replaced with a custom type in my use-case. Nonetheless,v1
compiles whilev2
does not. Usingv2 = j.get<decltype(v2)>();
solves the problem, albeit not that compact. I suspect this has to do with overloads fromstd
initialisation(?)nlohmann commentedon Jul 30, 2017
I can reproduce this with clang version 6.0.0 (trunk 309129):
nlohmann commentedon Jul 30, 2017
I hope @theodelrieu can help here.
theodelrieu commentedon Aug 1, 2017
Indeed, this happens when
T
has multipleoperator=
definitions. I don't think we can do anything about it, I personally always useget
in such cases.We should add an entry to the documentation explaining why this issue exists.
The feature is quite convenient, but code breaks when someone adds an
operator=
to a class.theodelrieu commentedon Aug 12, 2017
By the way, there is no need to add
from_json
method forstd::vector
and STL-types, the library already implement those (not to mention that your method will never be called by the library).📝 added note wrt. #667
nlohmann commentedon Aug 15, 2017
Updated the documentation.