Skip to content

[SR-12597] [C++] Import operator[] through some “raw” syntax #55043

@swift-ci

Description

@swift-ci
Previous ID SR-12597
Radar rdar://problem/62202421
Original Reporter martinboehme (JIRA User)
Type Sub-task
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Sub-task, CxxInterop
Assignee None
Priority Medium

md5: e1c4d75c0691c8353843d57d8fde29ec

Parent-Task:

  • SR-12595 [C++] Import a non-templated analog to std::string

cloned to:

  • SR-12748 [C++] Import overloaded operators (=, +, -, "", ...)

Issue Description:

We are planning to import `operator[]` as a Swift subscript (SR-12598) as that would be the most ergonomic option for most users. However, that import might not work great for all users. For example, when the C++ `operator[]` returns a reference, most users would want to immediately read or write through it. However, some would want to turn it into a pointer and persist for slightly longer. We should allow that use case in Swift.

struct Example {
  int &operator[](int index);
};

Example e;

// regular usage:
std::cout << e[10];
e[10] = 10;

// capturing a pointer:
int *ptr = &e[10];
...
*ptr = 10;

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++ interopFeature: Interoperability with C++compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions