# Exposing Swift protocols to C++ as pure abstract classes

**URL:** <https://forums.swift.org/t/exposing-swift-protocols-to-c-as-pure-abstract-classes/82528>\
**Category:** C++ interoperability\
**Created:** [October 6, 2025, 9:10am UTC](https://forums.swift.org/t/exposing-swift-protocols-to-c-as-pure-abstract-classes/82528 "2025-10-06T09:10:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![himeshi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/himeshi/32/43066_2.png) [@himeshi](https://forums.swift.org/u/himeshi)\
**Post date:** [October 6, 2025, 9:10am UTC](https://forums.swift.org/t/exposing-swift-protocols-to-c-as-pure-abstract-classes/82528/1 "2025-10-06T09:10:35Z")

</div>

Hello, everyone.

As I explore Swift C++ interoperability, I noticed that Swift protocols are not exposed to C++. Swift protocol is a very important way to abstract and constrain types, but that is currently invisible from C++ through C++ interoperability.

Let’s consider the following example. These are defined in Swift.

```swift
protocol Animal {
  func greet()
}

struct Dog: Animal {
  func greet() {
    print("bow wow")
  }
}
struct Cat: Animal {
  func greet() {
    print("meow")
  }
}

```

In Swift, abstracting these types are very easy ike this.

```swift
let animals: [any Animal] = [Dog(), Cat()]
animals.forEach { $0.greet() } // "bow wow", "meow"

```

However, in the current C++ interoperability, `Dog` and `Cat` are exposed as the completely irrelevant types. Since C++ does not have a feature like conforming to a protocol with Swift extension, it's hard for C++ to abstract these two types in the C++ layer.  
If the protocol `Animal` is exposed to C++ like this,

```cpp
class Animal {
  virtual void greet() = 0;
}

```

The concrete types can be exposed to C++ like this. And the abstraction will be easy too.

```cpp
class Dog: public Animal { ... }
class Cat: public Animal { ... }

std::vector<Animals> animals = { Dog::init(), Cat::init() };

```

It seems to be a reasonable approach to expose a Swift protocol as a pure abstract class of C++, since the role of these two are quite similar. Has this topic been considered before?

FYI: [C++ Abstract Class Inheritance and C++-Interop (to Swift Protocols)](https://forums.swift.org/t/c-abstract-class-inheritance-and-c-interop-to-swift-protocols/60170) discussed the opposite direction exposure, exposing a C++ abstract class to Swift as a protocol.

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [October 6, 2025, 12:58pm UTC](https://forums.swift.org/t/exposing-swift-protocols-to-c-as-pure-abstract-classes/82528/2 "2025-10-06T12:58:14Z")

</div>

> [@himeshi](#):
>
> `std::vector<Animals> animals = { Dog::init(), Cat::init() };`

Just as a warning, this isn't safe in C++, as if `Dog` and `Cat` have any stored properties they'll be dropped by this (object slicing). You would need to store a `std::vector<Animal*>` for it to be safe.

Would we have to add a `template<class T> struct SwiftExistential` to match Swift's expected layout for `any Animal`? Or would the compiler generate thunks to convert between Swift `any Animal` and C++ `Animal*`?

---

<div class="post-metadata">

**Author:** ![himeshi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/himeshi/32/43066_2.png) [@himeshi](https://forums.swift.org/u/himeshi)\
**Post date:** [October 6, 2025, 2:57pm UTC](https://forums.swift.org/t/exposing-swift-protocols-to-c-as-pure-abstract-classes/82528/3 "2025-10-06T14:57:52Z")

</div>

> [@bbrk24](#):
>
> Would we have to add a `template<class T> struct SwiftExistential` to match Swift's expected layout for `any Animal`? Or would the compiler generate thunks to convert between Swift `any Animal` and C++ `Animal*`?

For the existential type in C++, I think adding `SwiftExistential` type is reasonable, especially when we consider the case that one type which conforms the protocol is a struct and another is a class. In case of Swift class, the type is exposed to C++ as a kind of smart pointer like `std::shared_ptr<MyClassType>`, and it would be strange to handle this type as a raw pointer.
