dimanche 15 mars 2020

boost range join and temporary join_range object

Just started looking into Boost Range library but was not able to find answer in the documentation. The below code compiles using VS2017 and runs as expected (listing numbers 1 to 9). The issue is whether the implementation of my_range method is considered safe? The second boost::join expression is using ("jr") parameter from first boost::join which will go out of scope once method is finished. Any chance this method can returned an invalid boost::range::joined_range object?

Appreciate any input :-)

struct foo
{
  typedef boost::range::joined_range<
    boost::range::joined_range<
      std::vector<int>, 
      std::vector<int>>, 
    std::vector<int>> range;

  std::vector<int> v1;
  std::vector<int> v2;
  std::vector<int> v3;

  foo()
  {
    v1 = { 1,2,3 };
    v2 = { 4,5,6 };
    v3 = { 7,8,9 };
  }

  range my_range()
  {
    auto jr = boost::join(v1, v2); // (1)
    return boost::join(jr, v3);    // (2)
  }
};

int main()
{
  foo f1;
  for (auto item : f1.my_range())
    std::cout << item << " ";

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire