Why did MSVC emit the C24695 warning? #4538
-
I was using the MSVC toolchain and CMake on windows when I got a C26495 warning telling me that https://github.com/nlohmann/json/blob/620034e/include/nlohmann/json.hpp#L1142 Testing other compilers does not have this problem. code: #include <iostream>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
struct Person {
std::string name;
int age;
std::vector<std::string> hobbies;
friend void to_json(json& j, const Person& p) {
j = json{{"name", p.name}, {"age", p.age}, {"hobbies", p.hobbies}};
}
friend void from_json(const json& j, Person& p) {
j.at("name").get_to(p.name);
j.at("age").get_to(p.age);
j.at("hobbies").get_to(p.hobbies);
}
};
int main() {
Person person = {"John Doe", 30, {"Reading", "Traveling", "Swimming"}};
json j = person;
std::cout << "Serialized JSON:\n" << j.dump(4) << std::endl;
std::string serialized_json = j.dump();
json j2 = json::parse(serialized_json);
Person new_person = j2.get<Person>();
std::cout << "Deserialized Person:\n"
<< "Name: " << new_person.name << "\n"
<< "Age: " << new_person.age << "\n"
<< "Hobbies: ";
for (const auto& hobby : new_person.hobbies) {
std::cout << hobby << " ";
}
std::cout << std::endl;
}
https://godbolt.org/z/WTb78WvaW CMakeList.txt: find_package(nlohmann_json 3.2.0 REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json) Image: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
But I looked at the definition and it was initialized. Is this a BUG in MSVC? data m_data = {}; https://github.com/nlohmann/json/blob/620034e/include/nlohmann/json.hpp#L4221 |
Beta Was this translation helpful? Give feedback.
-
This is actually a bug of MSVC. Reduced and reported DevCom-10815325. |
Beta Was this translation helpful? Give feedback.
This is actually a bug of MSVC. Reduced and reported DevCom-10815325.