Repository navigation
json_pointer - a code example from doc's does not compile #4453
Copy link
Copy link
Closed
Labels
confirmeddocumentationkind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmationa fix for the issue has been proposed and waits for confirmation
Milestone
Description
Description
Gnu C++ does not compile the code example below. GCC 11.4.0, with -std=c++11 option
Example taken from https://json.nlohmann.me/features/json_pointer/#introduction
json::json_pointer p = "/nested/one";
main.cpp code found in "Minimal code example" field.
The _json_pointer literal is the only line that compiles correctly.
note: Clang compiler (clang++ v14.0.0) basically reports the same errors.
Reproduction steps
g++ main.cpp --std=c++11
Expected vs. actual results
JSON Pointer creation¶
JSON Pointers can be created from a string:
json::json_pointer p = "/nested/one";
Minimal code example
#include <string>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
std::string ptrStr = "/nested";
ptrStr += "/one";
json::json_pointer ptr1 = "/nested/one";
auto ptr2 = "/nested/one"_json_pointer;
json::json_pointer ptr3 = ptrStr;
}Error messages
GNU:
main.cpp: In function ‘int main()’:
main.cpp:11:31: error: conversion from ‘const char [12]’ to non-scalar type ‘nlohmann::json_abi_v3_11_3::basic_json<>::json_pointer’ {aka ‘nlohmann::json_abi_v3_11_3::json_pointer<std::__cxx11::basic_string<char> >’} requested
11 | json::json_pointer ptr1 = "/nested/one";
| ^~~~~~~~~~~~~
main.cpp:13:31: error: conversion from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to non-scalar type ‘nlohmann::json_abi_v3_11_3::basic_json<>::json_pointer’ {aka ‘nlohmann::json_abi_v3_11_3::json_pointer<std::__cxx11::basic_string<char> >’} requested
13 | json::json_pointer ptr3 = ptrStr;
| ^~~~~~
Clang:
main.cpp:11:24: error: no viable conversion from 'const char[12]' to 'json::json_pointer' (aka 'json_pointer<std::basic_string<char>>')
json::json_pointer ptr1 = "/nested/one";
^ ~~~~~~~~~~~~~
./json.hpp:13841:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char[12]' to 'const nlohmann::json_pointer<std::basic_string<char>> &' for 1st argument
class json_pointer
^
./json.hpp:13841:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const char[12]' to 'nlohmann::json_pointer<std::basic_string<char>> &&' for 1st argument
./json.hpp:13868:14: note: explicit constructor is not a candidate
explicit json_pointer(const string_t& s = "")
^
main.cpp:13:24: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'json::json_pointer' (aka 'json_pointer<std::basic_string<char>>')
json::json_pointer ptr3 = ptrStr;
^ ~~~~~~
./json.hpp:13841:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::string' (aka 'basic_string<char>') to 'const nlohmann::json_pointer<std::basic_string<char>> &' for 1st argument
class json_pointer
^
./json.hpp:13841:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::string' (aka 'basic_string<char>') to 'nlohmann::json_pointer<std::basic_string<char>> &&' for 1st argument
./json.hpp:13868:14: note: explicit constructor is not a candidate
explicit json_pointer(const string_t& s = "")
^
2 errors generated.Compiler and operating system
Ubuntu 22.04
Library version
3.11.1 and 3.11.3
Validation
- The bug also occurs if the latest version from the
developbranch is used. - I can successfully compile and run the unit tests.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
confirmeddocumentationkind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmationa fix for the issue has been proposed and waits for confirmation
Activity
dimitre commentedon Sep 11, 2024
I'm having a similar issue using json_pointers which we use extensively on openFrameworks projectGenerator
errors like this in code that used to work well before 3.11.3
kfwu1999 commentedon Sep 25, 2024
The compilation errors occur because the constructor of
json::json_pointeris marked asexplicit.Removing the
explicitkeyword allows the code to compile and pass the tests, but i think it might violate the design intentions.So, I think the better approach is to adjust the example code to use direct initialization.
Minimal code example (Modified)
github-actions commentedon Jan 19, 2025
This issue has been marked as stale because it has been open for 90 days without activity. If this issue is still relevant, please add a comment or remove the "stale" label. Otherwise, it will be closed in 10 days. Thank you for helping us prioritize our work!
nlohmann commentedon Jan 19, 2025
I can confirm the issue.
codenut commentedon Mar 4, 2025
Hello @nlohmann , I'm interested to work on this issue. Could you give me some hints on how to fix this?