0

I have some swift code, which is not really important in context of question. Also i have Objective-C bridging header ObjC-Swift-Bridging.h:

#import "my_objective_cpp_header.h"

And i have free C/C++ - style functions in that header:

inline void foo() {
}

inline void foo(int i) {
}

When i try to compile it in xcode 9.2 with enabled swift 4.0 i get error:

my_objective_cpp_header.h:29:13: error: redefinition of 'foo'
inline void foo(int i){
            ^
my_objective_cpp_header.h:26:13: note: previous definition is here
inline void foo() {
            ^
ObjC-Swift-Bridging.h:22:9: note: in file included from ObjC-Swift-Bridging.h:22:
#import "my_objective_cpp_header.h"

The same happens if i put these functions to C/C++ header and write

#include "my_cpp_header.h"

into ObjC-Swift-Bridging.h

So... Swift doesn't support code which was supported in Objective-C++? Did i miss something?

Hsilgos
  • 71
  • 1
  • 9
  • 2
    C has no function overloading. *"... which was supported in Objective-C"* – no, the same C declarations would fail to compile in an Objective-C source file. – Martin R Jun 28 '18 at 06:56
  • Well, yes. I rechecked this. May be you are right about Objective-C, but Objective-C++ should support this. At least my example with 'foo' functions is compiled in *.mm files. – Hsilgos Jun 28 '18 at 09:11
  • 1
    Swift *cannot* import (Objective-)C++ code, see for example https://stackoverflow.com/questions/35229149/interacting-with-c-classes-from-swift. – Martin R Jun 28 '18 at 09:12
  • I see. That's what i wanted to know. Thanks. – Hsilgos Jun 28 '18 at 09:13
  • 1
    @MartinR in fact, it isn't supported in Objective-C officially, but clang has an extension to allow that following C++ rules. I'm not sure about bridging this with Swift though. Hsilgos, have you tried with `__attribute__((overloadable))`? https://clang.llvm.org/docs/AttributeReference.html#overloadable-clang-overloadable-clang-overloadable – sidyll Jun 28 '18 at 17:46
  • Seems it works... But my case is a bit more complicated: i don't have access to original 'foo' method and i can't add this attribute to it (i only can add this attribute to my overloading function). But thanks, may be it will help me. – Hsilgos Jun 29 '18 at 19:14

0 Answers0