Skip to content

Overloaded bitwise negation operator (~) on C++ class not imported #83740

@Shortoz

Description

@Shortoz

Description

If a C++ class that defines an overload for the bitwise negation operator (~) is imported by Swift, that operator will not be available from Swift.

Reproduction

//CxxLibrary.h
class Foo {
	int bar;
	
public:
	Foo(int bar) : bar(bar) { }
	
	
	Foo operator ~() const {
		return Foo(~bar);
	}
};
import CxxLibrary

let negated = ~Foo(42) //error: referencing operator function '~' on 'BinaryInteger' requires that 'Foo' conform to 'BinaryInteger'

Expected behavior

The ~ operator can be used without error.

Environment

swift-driver version: 1.127.11.2 Apple Swift version 6.2 (swiftlang-6.2.0.16.14 clang-1700.3.16.4)
Target: x86_64-apple-macosx15.0

Additional information

Notably, if a C++ class defines an overload for the logical negation operator (!), it will be available.

//CxxLibrary.h
class Foo {
	int bar;
	
public:
	Foo(int bar) : bar(bar) { }
	
	
	Foo operator !() const {
		return Foo(!bar);
	}
};
import CxxLibrary

let negated = !Foo(42) //success

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions