Skip to content

ARC for C++ for easier interop #80931

@wtholliday

Description

@wtholliday

Motivation

Part of my codebase is C++ called from Swift and I've gotta do some refcounting myself.

Wouldn't it be great if C++ objects could be ARC'd like Swift and ObjC objects? Something like:

[[arc]] class MyCoolThing {
    ...
};

I wouldn't have to use smart pointers to manage the lifetime of MyCoolThing on the C++ side. Currently, I can't manage classes exposed to Swift using std::shared_ptr (right?) so I'd have to create my own smart pointer, or find some other intrusive reference counted smart pointer or just manually retain/release.

I also just think it would make C++ a less annoying language.

Proposed solution

Manage tagged C++ classes with ARC. Leverage code already used for ObjC.

Alternatives considered

  • a smart pointer which uses the same functions passed to SWIFT_SHARED_REFERENCE
  • std::shared_ptr (can't manually mutate the ref counts)
  • manually calling the retain/release functions passed to SWIFT_SHARED_REFERENCE

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ARCFeature: automatic reference countingc++ interopFeature: Interoperability with C++featureA feature request or implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions