You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This simple code crashes: string someStr; json j = json::object(); // the next line crashes j["someKey"] = someStr.empty() ? nullptr : someStr;
However, if I use standard if-else condition, everything is fine: string someStr; json j = json::object(); // the code below works ok if (someStr.empty()) j["someKey"] = nullptr; else j["someKey"] = someStr;
Also if S is not empty - both versions work.
Is it a bug or feature? :)
I'm using GNU C++ 14 compiler, IDE Xcode 9.3
The text was updated successfully, but these errors were encountered:
The types in someStr.empty() ? nullptr : someStr seems to be treated as const char* then yields j["someKey"] = (const char*)nullptr; which calls std::string(nullptr) which yields the error.
I have no idea how to tell the compiler that someStr.empty() ? nullptr : someStr should be treated as json other than to make it explicit:
A single type is deduced based on the types of the two values of the ternary operator. There is no type that works properly for nullptr and std::string here. That requirement is not there for the if/else. If you want the type to be null for an empty someStr instead of an empty string, you need to use the if/else. Otherwise, there's no point in even having the check, and you can simply do j["someKey"] = someStr;.
Yes, I indeed need the field being null for empty strings, and I was able to solve it.
Thank you, guys, for telling me two different solutions and for an information about how ternary operator works.
This simple code crashes:
string someStr;
json j = json::object();
// the next line crashes
j["someKey"] = someStr.empty() ? nullptr : someStr;
However, if I use standard if-else condition, everything is fine:
string someStr;
json j = json::object();
// the code below works ok
if (someStr.empty()) j["someKey"] = nullptr;
else j["someKey"] = someStr;
Also if S is not empty - both versions work.
Is it a bug or feature? :)
I'm using GNU C++ 14 compiler, IDE Xcode 9.3
The text was updated successfully, but these errors were encountered: