Skip to content

Documentation example code invalid for latest library version #628

Open

Description

@jmal0

From https://s2geometry.io/devguide/s2shapeindex.html:

void TestContainment(const vector<S2Point>& points,
                     const vector<S2Polygon*>& polygons) {
  MutableS2ShapeIndex index;
  for (auto polygon : polygons) {
    index.Add(absl::make_unique<S2Polygon::Shape>(polygon));
  }
  auto query = MakeS2ContainsPointQuery(&index);
  for (const auto& point: points) {
    for (S2Shape* shape : query.GetContainingShapes(point)) {
      S2Polygon* polygon = polygons[shape->id()];
      ... do something with (point, polygon) ...
    }
  }
}

This code does not compile for several reasons:

  • GetContainingShapes now returns std::vector<const S2Shape*>
  • There is no id() function in S2Shape (removed in this commit: 84bfd2c)

Activity

  1. rksharma-owg commented on Oct 14, 2026

    @rksharma-owg

    Confirmed against current master (677c8cebcff11ba11accc0b02005bc214e06e9fd): the published example still fails because GetContainingShapes() yields const S2Shape*, and S2Shape no longer has id().

    The existing VisitContainingShapeIds() API supplies the IDs directly, so the inner loop can preserve the original polygon lookup:

    query.VisitContainingShapeIds(point, [&](int shape_id) {
      S2Polygon* polygon = polygons[shape_id];
      // Use point and polygon here.
      return true;
    });

    Returning true keeps visiting matches. This lookup assumes the fresh index is populated in the same order as polygons, as in the example.

    I checked the original and corrected snippets with C++20, Apple Clang 21, and the repository-required Abseil LTS 20260526.0. The original reproduces both errors; the corrected version passes -fsyntax-only. Existing Abseil deprecation warnings remain; this was a compile check, not a runtime or full-library test.

    Where is the website's developer-guide source maintained so this correction can be submitted in the right place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions