Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templated from_json of non primitive types causes gcc error #2168

Closed
3 tasks
kouberl opened this issue Jun 4, 2020 · 3 comments
Closed
3 tasks

templated from_json of non primitive types causes gcc error #2168

kouberl opened this issue Jun 4, 2020 · 3 comments
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@kouberl
Copy link

kouberl commented Jun 4, 2020

What is the issue you have?

I use templated to_json/from_json methods, because I have different serializer for the same type.
I have problems with gcc with the from_json methods.

Please describe the steps to reproduce the issue.

When I use the json::get method it does not compile.

Can you provide a small but working code example?

#include <nlohmann/json.hpp>
#include <vector>

namespace ns {
    struct MyType {
        std::vector<int> i;
    };

    template <typename json_t>
    void to_json(json_t &j, const MyType &m) {
        j["i"] = m.i;
    }

    template <typename json_t> 
    void from_json(json_t const& j, MyType& m) {
        m.i = j.at("i").get<std::vector<int>>();
    }
}

int main() {
    ns::MyType type;
    type.i.push_back(42);
    nlohmann::json type_json = type;
    ns::MyType type2 = type_json;
}

As a workaround I inserted another copy: (toogle define to enable the workaround)
see https://godbolt.org/z/uVW_4b

What is the expected behavior?

should compile

And what is the actual behavior instead?

gcc error

Which compiler and operating system are you using?

  • Compiler: gcc
  • Operating system: linux

Which version of the library did you use?

  • [x ] latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • [ x] yes
  • no - please copy/paste the error message below
@nlohmann
Copy link
Owner

nlohmann commented Jun 5, 2020

Can you change

    template <typename json_t>
    void from_json(json_t const& j, MyType& m) {
        m.i = j.at("i").get<std::vector<int>>();
    }

to

    template <typename json_t>
    void from_json(json_t const& j, MyType& m) {
        m.i = j.at("i").template get<std::vector<int>>();
    }

Then it compiles for me - at least with Xcode.

@kouberl
Copy link
Author

kouberl commented Jun 5, 2020

Ok big thanks, this works. I always forget the template keyword.
Thanks!

@kouberl kouberl closed this as completed Jun 5, 2020
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 5, 2020
@nlohmann
Copy link
Owner

nlohmann commented Jun 5, 2020

I would have missed it unless Xcode hadn't given me a hint:

Screen Shot 2020-06-05 at 08 53 57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants