Podger2016 / rapidjson

Automatically exported from code.google.com/p/rapidjson
0 stars 0 forks source link

Pushing back a const Value reference gives a compilation error #94

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The generic version of the PushBack method fails when instantiated with T = 
const Value &. The reason is that it tries to construct a GenericValue wrapping 
around its first argument. In this case the method ends up calling the copy 
constructor, which is marked private.

This error can appear in the wild while trying to filter an array using 
Value::ConstValueIterator.

Original issue reported on code.google.com by lex.a...@gmail.com on 3 Jan 2014 at 10:38

GoogleCodeExporter commented 8 years ago
The error is currently intentional, as copying of values is not allowed.

With the enhancement proposal available in my personal GitHub fork [1] to add 
an explicit copy constructor (with an allocator), you can explicitly copy the 
filtered values to the destination document:

  Document dst(kArrayType), src;
  load_values(src);
  for (Value::ConstIterator it=src.Begin(), it!=src.End(); ++it)
    if (0 == *it.GetInt() % 2) { // only even values
      Value copy(*it,dst.GetAllocator);
      dst.PushBack(copy,dst.GetAllocator());
    }

I agree that the error message could be improved by adding a SFINAE condition 
for types supported by PushBack<T>.

hth,
Philipp

[1] https://github.com/pah/rapidjson/pull/2 (description)
    https://github.com/pah/rapidjson/compare/svn/trunk...value-copy-from.diff (patch)

Original comment by philipp....@gmail.com on 7 Apr 2014 at 1:24

GoogleCodeExporter commented 8 years ago
Fixed in https://github.com/miloyip/rapidjson/pull/20

Original comment by milo...@gmail.com on 13 Jul 2014 at 4:45