1

I need a function that returns all members of a rapidjson::Value as an std::vector. I am trying to avoid to write ifs with IsArray() everytime I need. Unfortunately the following code does not work.

std::vector<const rapidjson::Value&> valueToList(const rapidjson::Value& value)
{
    std::vector<const rapidjson::Value&> valueList;

    if (value.IsArray())
    {
        for (rapidjson::SizeType i = 0; i < value.Size(); i++)
        {
            valueList.push_back(val[i]);
        }
    }
    else
    {
        valueList.push_back(val);
    }

    return valueList;
}

I get the error push_back is ambiguous. Is there an easy way to overcome this? Thanks.

groove
  • 273
  • 2
  • 7
  • 17
  • 1
    You can't have a vector of native references. The second and third answers [**to this question**](http://stackoverflow.com/questions/922360/why-cant-i-make-a-vector-of-references) do a decent job of explaining why and proffering alternatives (mores than the selected answer to that question, imho). – WhozCraig Jan 07 '15 at 08:28

2 Answers2

2

The std::vector vector must be assignable, so you cannot have a std::vector of const references. Instead, try storing either rapidjson::Values or pointers to them. If you really want to store references, check out std::reference_wrapper.

From section 23.2.4 Class template vector of the C++ standard:

...the stored object shall meet the requirements of Assignable.

Călin
  • 347
  • 2
  • 13
0

You can't. Store pointers to the Values and you'll be fine. Make sure not to lose the rapidjson::Document.

Hame
  • 494
  • 4
  • 18