Skip to content

Compile Error on g++ using get() function #3827

@phil-zxx

Description

@phil-zxx

Description

When iterating over a nlohmann::json object and then casting the values using value.get<std::string>(), we observe that g++ gives a compile error. On clang and msvc everything compiles just fine.

Replacing value.get<std::string>() with std::string(value) fixes the problem on g++. But I wonder why the get function does not work?

Reproduction steps

See https://godbolt.org/z/Gxv7dbsjW

Expected vs. actual results

I would expect the "minimal code example" to compile as is on g++. But it does not.

Minimal code example

// https://godbolt.org/z/Gxv7dbsjW

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

template<class K, class V>
void convert(const nlohmann::json& sourceData, std::map<K, V>& targetMap)
{
    for (const auto& [key, value] : sourceData.items())
    {
        if constexpr(std::is_same_v<V, std::string>)
            targetMap.emplace(key, value.get<std::string>());
            //targetMap.emplace(key, std::string(value));  // Only way to fix g++ is to use this line
        else
            throw std::runtime_error("Case not covered");
    }
}

int main()
{
    const nlohmann::json data = {
        {"Year2", "2022" },
        {"Year3", "2023" },
        {"Year9", "2029" }
    };
    std::map<std::string, std::string> m;
    convert(data, m);

    for (const auto& [k, v] : m)
        std::cout << " - " << k << ": " << v << std::endl;

    return 0;
}

Error messages

<source>:10:42: warning: expected 'template' keyword before dependent template name [-Wmissing-template-keyword]
   10 |             targetMap.emplace(key, value.get<std::string>());
      |                                          ^~~
      |                                          template

<source>:10:57: error: expected primary-expression before '>' token
   10 |             targetMap.emplace(key, value.get<std::string>());
      |                                                         ^

<source>:10:59: error: expected primary-expression before ')' token
   10 |             targetMap.emplace(key, value.get<std::string>());
      |                                                           ^

Compiler and operating system

g++ (Linux)

Library version

3.11.1 & trunk

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions