-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Closed
Labels
kind: bugsolution: invalidthe issue is not related to the librarythe issue is not related to the library
Milestone
Description
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
- The bug also occurs if the latest version from the
develop
branch is used. - I can successfully compile and run the unit tests.
Metadata
Metadata
Assignees
Labels
kind: bugsolution: invalidthe issue is not related to the librarythe issue is not related to the library