# Is it possible to add \`swift\_attr\` to a class forward declaration outside of a 3rd party library?

**URL:** <https://forums.swift.org/t/is-it-possible-to-add-swift-attr-to-a-class-forward-declaration-outside-of-a-3rd-party-library/70029>\
**Category:** Using Swift\
**Tags:** cxx, cxx-interop, interoperability\
**Created:** [February 14, 2024, 4:30am UTC](https://forums.swift.org/t/is-it-possible-to-add-swift-attr-to-a-class-forward-declaration-outside-of-a-3rd-party-library/70029 "2024-02-14T04:30:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![felixfxu](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@felixfxu](https://forums.swift.org/u/felixfxu)\
**Post date:** [February 14, 2024, 4:30am UTC](https://forums.swift.org/t/is-it-possible-to-add-swift-attr-to-a-class-forward-declaration-outside-of-a-3rd-party-library/70029/1 "2024-02-14T04:30:49Z")

</div>

Hi,

I'm playing with swift's c++ interoperability using [GitHub - plotfi/SwiftBox2D: box2d Hello World ported to Swift using C++-Interop](https://github.com/plotfi/SwiftBox2D) as an example, it works but it is not perfect _in my point of view_: the solution needs to modify the 3rd party source code, see [SwiftBox2D/box2d-modifications/b2\_body.h.patch at main · plotfi/SwiftBox2D (github.com)](https://github.com/plotfi/SwiftBox2D/blob/main/box2d-modifications/b2_body.h.patch)

It modifies the `b2_body.h` file from

```cpp
class B2_API b2Body

```

to

```cpp
class B2_API IMMORTAL_REF b2Body

```

where `IMMORTAL_REF` is `swift_attr` is:

```cpp
#define IMMORTAL_REF \
      __attribute__ ((swift_attr("import_as_ref"))) \
      __attribute__ ((swift_attr("retain:immortal"))) \
      __attribute__ ((swift_attr("release:immortal")))

```

So basically, the solution needs to modify the source code and add `swift_attr`. I know this is a currently known issue for swift importing c++ libraries, but is it possible to have a better solution?

Currently I'm thinking of **forward declaration** in c++. Before we include any 3rd party code, we can forward declare the class like this:

```cpp
class IMMORTAL_REF b2Body;

```

This is not a definition but a forward declaration, it can be added **outside** the 3rd party library before including its header files. It's a good place to tell the swift compiler about the `swift_attr`. If it's possible then it will be a perfect solution and the 3rd party source code is untouched.

But it's not possible. After I add `IMMORTAL_REF` in the forward declaration but not in the class definition, I got this error:

```swift
box2d\b2_body.h:128:14: error: reference type 'b2Body' must have 'retain:' swift attribute
class B2_API b2Body
             ^

```

I guess it's because of the inconsistency of `IMMORTAL_REF` usage.

I also read that ` __attribute__ ` is compiler specific and never standardized (maybe I'm wrong), therefore can we make it possible to add `swift_attr` in forward declaration? It will be a compiler change I'm afraid.

---

<div class="post-metadata">

**Author:** ![geertb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/geertb/32/34401_2.png) [@geertb](https://forums.swift.org/u/geertb)\
**Post date:** [March 11, 2024, 10:27am UTC](https://forums.swift.org/t/is-it-possible-to-add-swift-attr-to-a-class-forward-declaration-outside-of-a-3rd-party-library/70029/2 "2024-03-11T10:27:37Z")

</div>

I was trying to do the same thing as well when creating a C++/Swift mixed library.  
In the process of replacing objc++ sources with Swift equivalents, I noticed that the lifetime of externally defined types (in my case ImGui types) had gotten value type semantics, which breaks its behaviour. So I thought that adding these attributes in the bridging header could solve it.  
However, it is impossible to get them to have reference type semantics without having the modify the shared (it's used by other parts of the project as well) 3rd party library.

Interestingly enough, it gives a specific warning when misplacing the SWIFT\_SHARED\_REFERENCE macro in context of type declaration:

 ![image](https://global.discourse-cdn.com/swift/original/3X/6/7/677d64660869e793457bdd97998f017d569f5391.png)  
So to some extent, the compiler is aware about adding attributes to type declarations.

---

<div class="post-metadata">

**Author:** ![pusewicz](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pusewicz/32/36177_2.png) [@pusewicz](https://forums.swift.org/u/pusewicz)\
**Post date:** [July 28, 2025, 9:41pm UTC](https://forums.swift.org/t/is-it-possible-to-add-swift-attr-to-a-class-forward-declaration-outside-of-a-3rd-party-library/70029/3 "2025-07-28T21:41:18Z")

</div>

Maybe API Notes would be useful here? [swift/apinotes/README.md at main · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/blob/main/apinotes/README.md)

---

<div class="post-metadata">

**Author:** ![felixfxu](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@felixfxu](https://forums.swift.org/u/felixfxu)\
**Post date:** [October 10, 2025, 2:56am UTC](https://forums.swift.org/t/is-it-possible-to-add-swift-attr-to-a-class-forward-declaration-outside-of-a-3rd-party-library/70029/4 "2025-10-10T02:56:17Z")

</div>

yes, [API Notes: Annotations Without Modifying Headers — Clang 22.0.0git documentation](https://clang.llvm.org/docs/APINotes.html) is exactly what I need
