Open kikoalbiol opened 6 years ago
Is there an explicit reason const
wasn't used here? Without it, you can't use a const std::string
for both the name and value. For example:
std::string str = "non-const string";
const std::string cStr = "const string";
Document d;
auto& alloc = d.GetAllocator();
d.AddMember(Value(cStr, alloc).Move(), str, alloc); //< works
d.AddMember(Value(cStr, alloc).Move(), cStr, alloc); //< doesn't compile
This looks like an oversight to me. Can you submit a pull-request?
// GenericObject:
GenericObject AddMember(ValueType& name, const std::basic_string<Ch>& value, AllocatorType& allocator)
{ value_.AddMember(name, value, allocator); return *this; }
// GenericValue:
GenericValue& AddMember(GenericValue& name, const std::basic_string<Ch>& value, Allocator& allocator)
Looks like the reason it's this way is because if you add const std::basic_string&
then you now can't resolve this case:
Document d;
auto& alloc = d.GetAllocator();
Value a("a");
Value b("b");
d.AddMember(a, "A", alloc);
d.AddMember(b, std::string("B"), alloc);
You get an error like this because "A"
is convertible to StringRefType
and const std::string&
:
test.cpp: error: call to member function 'AddMember' is ambiguous
d.AddMember(a, "A", alloc);
~~^~~~~~~~~
/Users/kersson/dev/rapidjson/include/rapidjson/document.h:1340:19: note: candidate function
GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) {
^
/Users/kersson/dev/rapidjson/include/rapidjson/document.h:1355:19: note: candidate function
GenericValue& AddMember(GenericValue& name, const std::basic_string<Ch>& value, Allocator& allocator) {
^
So I guess we're stuck with it? 😢
The suggested definition is: