Files
swift-mirror/stdlib/public/Cxx/CxxConvertibleToBool.swift
Egor Zhdan f0be52accd [cxx-interop] Add conversion to Bool for types that define operator bool()
C++ `operator bool()` is currently imported into Swift as `__convertToBool()`, which shouldn't be used by clients directly.

This adds a new protocol into the C++ stdlib overlay: `CxxConvertibleToBool`, along with an intitializer for `Swift.Bool` taking an instance of `CxxConvertibleToBool`.

rdar://115074954
2023-09-18 14:54:45 +01:00

27 lines
938 B
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A C++ type that can be converted to a Boolean value.
///
/// Any C++ type that defines `operator bool()` conforms to this protocol.
public protocol CxxConvertibleToBool {
/// Do not implement this function manually in Swift.
func __convertToBool() -> Bool
}
extension Bool {
@inlinable
public init<B: CxxConvertibleToBool>(fromCxx convertible: __shared B) {
self = convertible.__convertToBool()
}
}