Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

vector_indexing_suite seems to break the use of return_internal_reference

Offen
#299 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
cpp, python

Rechercherichtung

Beginne mit dem verknüpften Boost.Python-Beispiel zu return_internal_reference und erstelle das gezeigte C++-Modul, das Foo, Bar und FooList bereitstellt. Führe die Python-Sequenz aus, die ein zweites Foo anhängt, und untersuche anschließend, warum sich die zuvor zurückgegebene Bar-Referenz ändert, während foo_ref.get_bar() weiterhin gültig bleibt. Als abgeschlossen gilt die Aufgabe, wenn die interne Referenz nach dem Wachstum des Vektors gültig bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

I can create a class Foo that returns an internal reference to a class Bar and everything seems to work just fine. However, when I try and expose a vector of Foo using the vector_indexing_suite, I get some weird behavior. In Python, a reference to the underlying Bar of a Foo in a vector of Foos gets corrupted when a new Foo is appended to the vector.

Since most of the code comes straight out of the Boost Python docs, I assume it should work.

The issue can be replicated by making a few modifications to an example from the Boost Python docs.

#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>


#include <vector>

class Bar
{
 public:
   Bar(int x) : x(x) {}
   int get_x() const { return x; }
   void set_x(int x) { this->x = x; }

   bool operator==(const Bar &other) const { return other.x == x;}
   bool operator!=(const Bar &other) const { return !(other == (*this)); }

 private:
   int x;
};

class Foo
{
 public:
   Foo(int x) : b(x) {}

   // Returns an internal reference
   Bar const& get_bar() const { return b; }

   bool operator==(const Foo &other) const {return other.b == b;}
   bool operator!=(const Foo &other) const { return !(other == (*this)); }

 private:
   Bar b;
};

using namespace boost::python;
BOOST_PYTHON_MODULE(boosttest)
{
   class_<Bar>("Bar", init<int>())
      .def("get_x", &Bar::get_x)
      .def("set_x", &Bar::set_x)
      ;

   class_<Foo>("Foo", init<int>())
      .def("get_bar", &Foo::get_bar
          , return_internal_reference<>())
      ;

   class_<std::vector<Foo>>("FooList")
      .def(vector_indexing_suite<std::vector<Foo>>())
      ;
}

Then on the Python side, we get the following.

>>> import boosttest
>>> foolist = boosttest.FooList()
>>> foolist.append(boosttest.Foo(2))
>>> foo_ref = foolist[0]
>>> bar_ref = foo_ref.get_bar()
>>> bar_ref.get_x()
2
>>> foolist.append(boosttest.Foo(3))
>>> bar_ref.get_x()
-572662307
>>> foo_ref.get_bar().get_x()
2
Vorherrschende Sprache
C++
Sterne
537
Forks
223
Ø Merge
11 Std. 22 Min.
Gemergte PRs (30 T.)
2

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus boostorg/python

Alle Issues in boostorg/python

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.